Skip to content

Instantly share code, notes, and snippets.

@cangoal
Last active March 16, 2016 02:13
Show Gist options
  • Save cangoal/33be02147ae62ba72905 to your computer and use it in GitHub Desktop.
Save cangoal/33be02147ae62ba72905 to your computer and use it in GitHub Desktop.
LeetCode - Remove Duplicates from Sorted List
//
public ListNode deleteDuplicates(ListNode head) {
if(head == null) return head;
ListNode pre = head, cur = head.next;
while(cur != null){
if(cur.val != pre.val){
cur = cur.next;
pre = pre.next;
} else {
pre.next = cur.next;
cur = pre.next;
}
}
return head;
}
//
public ListNode deleteDuplicates(ListNode head) {
ListNode ret = head;
if(head == null)
return head;
while(head.next != null){
ListNode p1 = head.next;
if(p1.val == head.val){
head.next = p1.next;
}else{
head = head.next;
}
}
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment