剑指offer-56.删除链表中重复的结点

来源:互联网 发布:龙虎榜数据分析软件 编辑:程序博客网 时间:2024/06/06 03:37

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


思路:进行递归实现



/*struct ListNode {    int val;    struct ListNode *next;    ListNode(int x) :        val(x), next(NULL) {    }};*/class Solution {public:    ListNode* deleteDuplication(ListNode* pHead)    {        if ( pHead == NULL || pHead->next == NULL)            return pHead;        ListNode *pNode;       if (pHead->val == pHead->next->val)       {          pNode  = pHead->next->next;           while (pNode != NULL && pNode->val == pHead->val)           {               pNode = pNode->next;           }           return deleteDuplication(pNode);       }       else       {           pNode = pHead->next;           pHead->next = deleteDuplication(pNode);           return pHead;       }            }};


0 0
原创粉丝点击