删除链表中重复的结点

python

class Solution:
    def deleteDuplication(self,pHead):
        if not pHead or not pHead.next:
            return pHead
        //返回无重复第一个节点
        if pHead.val == pHead.next.val:
            pnode = pHead.next.next
            while pnode and pnode.val==pnode.next.val:
                pnode = pnode.next.next
            //这个时候第一个pnode.val != pnode.next.val
            //return 的意思是,再次迭代pnode.val==pnode.next.val的情况,一直到没有相等的时候执行else
            #此时应该会相同所有相同结点的下一个,这个时候继续将这个节点进行迭代
            #注意,要返回的是删除了相同结点后下一个结点
            return self.deleteDuplication(pnode)
        else:
            #如果这个节点和下一个结点不一样,那就继续把下一个结点送入到函数中
            #返回的应该是不同节点的第一个个,所以返回phead
            pHead.next = self.deleteDuplication(pHead.next)
            return pHead
    

 

c++

 

 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           // 链表有0个/1个节点,返回第一个节点
15           if(pHead==NULL||pHead->next==NULL)
16               return pHead;
17           else
18           {
19               // 新建一个头节点,防止第一个结点被删除
20               ListNode* newHead=new ListNode(-1);
21               newHead->next=pHead;
22 
23               // 建立索引指针
24               ListNode* p=pHead;      // 当前节点
25               ListNode* pre=newHead;  // 当前节点的前序节点
26               ListNode* next=p->next;    // 当前节点的后序节点
27 
28               // 从头到尾遍历编标
29               while(p!=NULL && p->next!=NULL)
30               {
31                   if(p->val==next->val)//如果当前节点的值和下一个节点的值相等
32                   {
33                       // 循环查找,找到与当前节点不同的节点
34                       while(next!=NULL && next->val==p->val)
35                       {
36                           ListNode* temp=next;
37                           next=next->next;
38 
39                           // 删除内存中的重复节点
40                           delete temp;//delete指针之后,只是回收指针指向位置的空间,指针的值不变,所以还要讲指针设置为null
41                           temp = nullptr;//这样可以避免野指针的出现
42 
43                       }
44 
45                     pre->next=next;
46                     p=next;
47                   }
48                   else//如果当前节点和下一个节点值不等,则向后移动一位
49                   {
50                       pre=p;
51                       p=p->next;
52                   }
53                   next=p->next;
54               }
55            return newHead->next;//返回头结点的下一个节点
56           }
57     }
58 };

 自己修改了一点点

 1 struct ListNode {
 2     int val;
 3     struct ListNode *next;
 4     ListNode(int x) :
 5         val(x), next(NULL) {
 6     }
 7 };
 8 */
 9 class Solution {
10 public:
11     ListNode* deleteDuplication(ListNode* pHead)
12     {
13         if(!pHead || !pHead->next) return pHead;
14         ListNode* new_head = new ListNode(-1);
15         new_head->next = pHead;
16         ListNode* pre = new_head;
17         ListNode* cur = pHead;
18         ListNode* nex = pHead->next;
19         while(cur && nex){
20             if(cur->val ==nex->val){
21                 while(nex && nex->val==cur->val){
22                     ListNode* temp = nex;
23                     nex = nex->next;
24                     delete temp;//delete只是删除了指针指向的位置的空间,指针本身的值没有改变,所以要再设置成null,不然出现野指针现象
25                     temp = nullptr;
26                 }
27                 pre->next = nex;
28                 cur = nex;
29                 nex = nex->next;
30             }
31             else{
32                 pre = cur;
33                 cur = nex;
34                 nex = nex->next;
35             }
36         }
37         pHead = new_head->next;
38         delete new_head;
39         return pHead;
40     }
41 };

 

posted @ 2019-06-25 19:59  Austin_anheqiao  阅读(247)  评论(0编辑  收藏  举报