Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created July 16, 2016 04:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/0318844f35723d6e28d9a90092b2a85c to your computer and use it in GitHub Desktop.
Save jianminchen/0318844f35723d6e28d9a90092b2a85c to your computer and use it in GitHub Desktop.
Remove duplicate in sorted linked list - not optimized for run time - could not figure out - 2nd practice
/**
* Definition for singly-linked list.
* class ListNode {
* public int val;
* public ListNode next;
* ListNode(int x) { val = x; next = null; }
* }
*/
public class Solution {
public ListNode deleteDuplicates(ListNode a) {
if(a==null)
return null;
if(a.next == null)
return a;
ListNode runner = a;
while(true)
{
// skip duplicate node, such as 1, 1, 1, 2, skip second 1, third 1
ListNode start = runner;
while( start.val == runner.next.val)
{
runner = runner.next;
if(runner == null)
{
return a;
}
}
start.next = runner;
if(runner.next == null)
return a;
}
return a;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment