在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5
- 代码1
# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def deleteDuplication(self, pHead):
# write code here
res = []
while pHead:
res.append(pHead.val)
pHead = pHead.next
# filter函数和map相似,但是filter是返回布尔值去去输入列表进行判断
res = list(filter(lambda c: res.count(c) == 1, res))
newlist = ListNode(0)
pre = newlist
for i in res:
node = ListNode(i)
pre.next = node
pre = pre.next
return newlist.next
- 代码2
# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def deleteDuplication(self, pHead):
# write code here
nodeValList = []
while pHead:
nodeValList.append(pHead.val)
pHead = pHead.next
res = []
for i in nodeValList:
if nodeValList.count(i) == 1:
res.append(i)
newlist = ListNode(0)
pre = newlist
for i in res:
node = ListNode(i)
pre.next = node
pre = pre.next
return newlist.next
- 代码3
# -*- coding:utf-8 -*-
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def deleteDuplication(self, pHead):
if not pHead:
return None
nodeList = []
nodeValList = []
nodePointer = pHead
while nodePointer:
if nodePointer.val not in nodeValList:
nodeList.append(nodePointer)
nodeValList.append(nodePointer.val)
elif nodeList[-1].val == nodePointer.val:
nodeList.pop()
nodePointer = nodePointer.next
for i in range(len(nodeList)-1):
nodeList[i].next = nodeList[i+1]
return nodeList[0]