删除链表中重复的结点(python)
程序员文章站
2024-03-06 09:58:07
...
Leetcode. 82. Remove Duplicates from Sorted List II
题目
在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。
例如:
输入:1->2->3->3->3->4->4->5->6
输出:1->2->5->6
class Solution(object):
def deleteDuplicates(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
tempNode = pre = ListNode(0)
tempNode.next = head
while head and head.next:
if head.val == head.next.val:
while head and head.next and head.val == head.next.val:
head = head.next
head = head.next
pre.next = head
else:
pre = pre.next
head = head.next
return tempNode.next
83. Remove Duplicates from Sorted List
相似题目
在一个排序的链表中,存在重复的结点,请删除该链表中多余重复的结点,重复的结点仅保留一个,返回链表头指针。
例如:
输入:1->2->3->3->3->4->4->5->6
输出:1->2->3->4->5->6
class Solution(object):
def deleteDuplicates(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
tempNode = pre = ListNode(0)
tempNode.next = head
while head and head.next:
if head.val == head.next.val:
head = head.next
pre.next = head
else:
pre = pre.next
head = head.next
return tempNode.next