leetcode - Remove Duplicates from Sorted List II
2013-11-27 08:38 张汉生 阅读(164) 评论(0) 编辑 收藏 举报
1 class Solution { 2 public: 3 ListNode *deleteDuplicates(ListNode *head) { 4 // IMPORTANT: Please reset any member data you declared, as 5 // the same Solution instance will be reused for each test case. 6 if (head==NULL) 7 return head; 8 ListNode * curHead = head, *curTail = head, *lastTail=NULL; 9 ListNode * itr = head->next; 10 while (itr!=NULL){ 11 ListNode * nextNode = itr->next; 12 if (itr->val == curTail->val) 13 curTail = curTail->next; 14 else{ 15 if (curHead!=curTail){ 16 if (curHead==head){ 17 head = itr; 18 } 19 else lastTail->next = itr; 20 } 21 else lastTail = curTail; 22 curHead = curTail = itr; 23 } 24 itr = nextNode; 25 } 26 if (curHead != curTail){ 27 if (curHead == head) 28 return NULL; 29 else lastTail->next = NULL; 30 } 31 return head; 32 } 33 };