Skip to content

Instantly share code, notes, and snippets.

@Cee
Created May 28, 2014 12:48
Show Gist options
  • Save Cee/eaf0a8d3e4004d725e0f to your computer and use it in GitHub Desktop.
Save Cee/eaf0a8d3e4004d725e0f to your computer and use it in GitHub Desktop.
/**
* 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