1 public class Solution { 2 public ListNode deleteDuplicates(ListNode head) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(head == null || head.next == null) 6 return head; 7 ListNode fakehead = new ListNode(-1); 8 fakehead.next = head; 9 ListNode runner = head; 10 ListNode prerunner = fakehead; 11 boolean isDup = false; 12 int tmp = 0; 13 while(runner!=null){ 14 if(!isDup){ 15 if(runner.next!=null && runner.val == runner.next.val){ 16 tmp = runner.val; 17 isDup = true; 18 runner = runner.next; 19 prerunner.next = runner; 20 } 21 else{ 22 runner = runner.next; 23 prerunner = prerunner.next; 24 } 25 } 26 else{ 27 if(runner.val == tmp){ 28 runner = runner.next; 29 prerunner.next = runner; 30 } 31 else{ 32 isDup = false; 33 } 34 } 35 } 36 37 prerunner.next = null; 38 return fakehead.next; 39 } 40 }