[剑指Offer] 56.删除链表中重复的结点

题目描述

在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5

【思路】递归

 1 /*
 2 struct ListNode {
 3     int val;
 4     struct ListNode *next;
 5     ListNode(int x) :
 6         val(x), next(NULL) {
 7     }
 8 };
 9 */
10 class Solution {
11 public:
12     ListNode* deleteDuplication(ListNode* pHead)
13     {
14         if((pHead == NULL)||(pHead != NULL && pHead->next == NULL))
15             return pHead;
16         ListNode* current;
17         if(pHead->val == pHead->next->val){
18             current = pHead->next->next;
19             while(current != NULL && current->val == pHead->val)
20                 current = current->next;
21             return deleteDuplication(current);
22         }else{
23             current = pHead->next;
24             pHead->next = deleteDuplication(current);
25             return pHead;
26         }
27     }
28 };

 

posted @ 2017-03-17 18:39  Strawberry丶  阅读(157)  评论(0编辑  收藏  举报