Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given
Given
Given
1->2->3->3->4->4->5
, return 1->2->5
.Given
1->1->1->2->3
, return 2->3
.
public class Solution { public ListNode deleteDuplicates(ListNode head) { // Start typing your Java solution below // DO NOT write main() function ListNode prev = new ListNode(0); prev.next = head; head = prev; ListNode n1=head; while(n1.next!=null){ ListNode n2=n1.next; while(n2.next!=null && n2.next.val==n2.val){ n2=n2.next; } if(n2!=n1.next){ n1.next=n2.next; }else{ n1=n1.next; } } return head.next; } }
3 comments:
Thank you!
what if the list is 1->1 ? the expect result should be null, but will this algorithm return 1?
With a safe guard node at the front of the list, it will not return 1.
Post a Comment