Link: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-list/
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given 1->1->2
, return 1->2
.
Given 1->1->2->3->3
, return 1->2->3
.
1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) { 7 * val = x; 8 * next = null; 9 * } 10 * } 11 */ 12 public class Solution { 13 public ListNode deleteDuplicates(ListNode head) { 14 if(head==null) 15 return head; 16 ListNode p1 = head; 17 ListNode p2 = head; 18 ListNode result = new ListNode(0); 19 ListNode result_point = result; 20 while(p2!=null){ 21 while(p2!=null&&p2.val==p1.val) 22 { 23 p2 = p2.next; 24 } 25 result.next = p1; 26 result = result.next; 27 p1 = p2; 28 } 29 result.next = null; 30 return result_point.next; 31 } 32 }