Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given
Given
Given
1->1->2
, return 1->2
.Given
1->1->2->3->3
, return 1->2->3
.public class Solution { public ListNode deleteDuplicates(ListNode head) { // Start typing your Java solution below // DO NOT write main() function if(head==null) return head; ListNode n1= head; while(n1.next!=null){ if(n1.next.val==n1.val){ n1.next=n1.next.next; }else{ n1=n1.next; } } return head; } }
No comments:
Post a Comment