删除链表中重复(数字域)的节点

 1 # -*- coding:utf-8 -*-
 2 # class ListNode:
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.next = None
 6 """没有三个指针解决不了的问题"""
 7 class Solution:
 8     def deleteDuplication(self, pHead):##最好以原链表指针为输出
 9         # write code here
10         pPre = None
11         pCur = pHead#给链表加个指针
12         pNext = None
13         while pCur != None:   
14             if pCur.next != None and pCur.val == pCur.next.val:  #如果不到尾节点,并且当前指针节点与下一节点数值相等时
15                 pNext = pCur.next#更新next指针
16                 while pNext.next != None and pNext.next.val == pCur.val: #当next指针没到尾节点,且next下一节点数值与当前节点值相等时
17                     pNext = pNext.next      #一直更新next指针
18                 if pCur == pHead:    #处理第一个节点就是重复节点的情况
19                     pHead = pNext.next #直接将原链表pHead指针后移到一段重复的节点之后
20                 else:
21                     pPre.next = pNext.next   #如果头结点不是重复节点,则移动pre指针
22                 pCur = pNext.next   #更新当前指针到一段重复节点之后节点
23             else:     #更新前指针pre到现指针cur上
24                 pPre = pCur
25                 pCur = pCur.next #更新当前指针cur
26         return pHead

 

posted @ 2019-05-08 02:18  Parallax  阅读(151)  评论(0编辑  收藏  举报