删除链表中重复结点,使每个结点最多出现2次
class Solution(object):
def removeDuplicates(self, head):
"""
:type head: ListNode
"""
if not head or not head.next:
return head
low = head
fast = head.next
while fast:
if fast.next and fast.val == low.val and fast.val == fast.next.val:
fast.next = fast.next.next
else:
low = fast
fast = fast.next
return head
本文介绍了一种算法,用于删除链表中重复出现的节点,确保每个节点最多只出现两次。通过使用两个指针,low 和 fast,来遍历链表并移除多余的重复节点。

513

被折叠的 条评论
为什么被折叠?



