题目描述:在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5
思路:两次遍历,第一次记录重复出现的value,第二次删除有重复出现value的节点。
# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def deleteDuplication(self, pHead):
occured = set()
repeat = set()
if(pHead == None):
return pHead
tmp = pHead
while(tmp != None):
if(tmp.val in occured):
repeat.add(tmp.val)
else:
occured.add(tmp.val)
tmp = tmp.next
tmp = pHead
while(tmp.next != None):
if(tmp.next.val in repeat):
tmp.next = tmp.next.next
else:
tmp = tmp.next
if(pHead.val in repeat):
pHead = pHead.next
return pHead