Created
May 28, 2014 12:48
-
-
Save Cee/eaf0a8d3e4004d725e0f to your computer and use it in GitHub Desktop.
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
/** | |
* Definition for singly-linked list. | |
* public class ListNode { | |
* int val; | |
* ListNode next; | |
* ListNode(int x) { | |
* val = x; | |
* next = null; | |
* } | |
* } | |
*/ | |
public class Solution { | |
public ListNode deleteDuplicates(ListNode head) { | |
ListNode ret = null; | |
ListNode p = head; | |
ListNode ret_tail = null; | |
boolean flag = true; | |
while (p != null){ | |
if ((p.next == null) || ((p.next != null) && (p.next.val != p.val))){ | |
if (flag){ | |
ListNode q = new ListNode(p.val); | |
if (ret == null){ | |
ret = q; | |
ret_tail = ret; | |
}else{ | |
ret_tail.next = q; | |
ret_tail = ret_tail.next; | |
} | |
}else{ | |
flag = true; | |
} | |
}else{ | |
flag = false; | |
} | |
p = p.next; | |
} | |
return ret; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment