class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class List:
def init_list(self, data):
# 判断是否为空
if len(data) == 0:
return None
else:
# 头结点
head = ListNode(data[0])
# 尾节点
tail = head
# 逐个为 data 内的数据创建结点, 建立链表
for i in data[1:]:
node = ListNode(i)
tail.next = node
tail = node
return head
def print(self, head):
while head:
print(head.val, end="->")
head = head.next
print(None)
a = [1,1,2,3,3]
l = List()
res = l.init_list(a)
l.print(res)
class Solution:
def deleteDuplicates(self, head):
if head:
slow = head
fast = head.next
while slow and fast:
if slow.val == fast.val:
fast = fast.next
slow.next =fast
else:
slow = fast
fast = fast.next
return head
return head
head = res
ll = Solution().deleteDuplicates(head)
l.print(ll)
删除排序链表中的重复元素
最新推荐文章于 2024-10-31 13:56:46 发布