首页 > 技术文章 > Remove Duplicates from Sorted List

waruzhi 2013-09-22 19:45 原文

基本的链表操作,只需一次遍历即可

 1     ListNode *deleteDuplicates(ListNode *head) {
 2         if(!head)
 3             return NULL;
 4         ListNode *cur, *next;
 5         cur = head;
 6         while(cur){
 7             next = cur->next;
 8             while(next){
 9                 if(next->val != cur->val)
10                     break;
11                 next = next->next;
12             }
13             cur->next = next;
14             cur = cur->next;
15         }
16         return head;
17     }

 

推荐阅读