题目:
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
.
思路与分析:直接遍历
代码:
public static ListNode deleteDuplicates(ListNode head) { if(head == null){ return null; } int intFlag = head.val; //ListNode listCurrent = head.next; ListNode listCurrent = head; while(listCurrent.next != null){ if(intFlag == listCurrent.next.val){ if(listCurrent.next.next != null){ listCurrent.next = listCurrent.next.next; } else{ listCurrent.next = null; } } else{ intFlag = listCurrent.next.val; listCurrent = listCurrent.next; } } return head; }