# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
newHead = ListNode(-1)
if not head:
return head
p = newHead
cur = head
post = cur.next
p.next = cur
while cur and post:
if cur.val != post.val:
p = cur
cur = post
post = post.next
else:
val = cur.val
while cur and cur.val == val:
cur = post
if post:
post = post.next
p.next = cur
return newHead.next
本文讲解了如何使用Python实现删除单链表中重复节点,通过实例演示了如何构造新的链表结构,确保每个值只出现一次。
444

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



