Created
July 16, 2016 04:55
-
-
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
This file contains 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. | |
* 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