标签:
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 class Solution { 2 public: 3 ListNode* deleteDuplicates(ListNode* head) { 4 ListNode * p = head; 5 if(p == NULL || p->next == NULL) 6 return p; 7 ListNode * prev = p; 8 p = p->next; 9 while(p!=NULL){ 10 if(p->val == prev->val){ 11 prev->next = p->next; 12 }else{ 13 prev = p; 14 } 15 p = p->next; 16 } 17 return head; 18 } 19 };
LeetCode OJ:Remove Duplicates from Sorted List (排好序的链表去重)
标签:
原文地址:http://www.cnblogs.com/-wang-cheng/p/4878798.html