1. 程式人生 > >劍指offer-刪除鏈表中重復的結點

劍指offer-刪除鏈表中重復的結點

div 排序 例如 lse tle scribe while else null

題目描述

在一個排序的鏈表中,存在重復的結點,請刪除該鏈表中重復的結點,重復的結點不保留,返回鏈表頭指針。 例如,鏈表1->2->3->3->4->4->5 處理後為 1->2->5
 1 public ListNode deleteDuplication(ListNode pHead)
 2     {//鏈表 my
 3         ListNode head = new ListNode(0);
 4         head.next =pHead;
 5         ListNode cur = pHead;
6 ListNode pre = head; 7 while(null!=cur&&null!=cur.next){ 8 if(cur.next.val!=cur.val){ 9 cur = cur.next; 10 pre = pre.next; 11 } 12 else{ 13 int val = cur.val; 14 while
(null!=cur && cur.val ==val){ 15 pre.next = cur.next; 16 cur = cur.next; 17 } 18 } 19 } 20 return head.next; 21 }

劍指offer-刪除鏈表中重復的結點