Last active
March 16, 2016 02:12
-
-
Save cangoal/2af832cbae19a025727b to your computer and use it in GitHub Desktop.
LeetCode - Remove Duplicates from Sorted List II
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| public ListNode deleteDuplicates(ListNode head) { | |
| if(head == null) return head; | |
| ListNode dummyNode = new ListNode(0); | |
| dummyNode.next = head; | |
| ListNode pre = dummyNode; | |
| while(head != null && head.next != null){ | |
| if(head.val == head.next.val){ | |
| while(head.next != null && head.val == head.next.val){ | |
| head = head.next; | |
| } | |
| pre.next = head.next; | |
| } else { | |
| pre = pre.next; | |
| } | |
| head = pre.next; | |
| } | |
| return dummyNode.next; | |
| } | |
| // | |
| public ListNode deleteDuplicates(ListNode head) { | |
| if(head==null || head.next==null) return head; | |
| ListNode dummy = new ListNode(0); | |
| ListNode pre = dummy; | |
| pre.next = head; | |
| while(head != null && head.next != null){ | |
| if(head.val!=head.next.val){ | |
| pre = pre.next; | |
| head = head.next; | |
| }else{ | |
| int val = head.val; | |
| while(head!=null && head.val==val){ | |
| head = head.next; | |
| } | |
| pre.next = head; | |
| } | |
| } | |
| return dummy.next; | |
| } | |
| // | |
| public ListNode deleteDuplicates(ListNode head) { | |
| if(head == null) | |
| return head; | |
| ListNode dummy = new ListNode(0); | |
| ListNode pre = dummy; | |
| pre.next = head; | |
| ListNode cur = head; | |
| while(cur != null){ | |
| while(cur.next != null && cur.val == cur.next.val){ | |
| cur = cur.next; | |
| } | |
| if(pre.next == cur) | |
| pre = pre.next; | |
| else | |
| pre.next = cur.next; | |
| cur = cur.next; | |
| } | |
| return dummy.next; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment