Problem 2.1: Remove Duplicates (100 pts)
Problem
Write a function
remove_duplicateswhich takes in a sorted linked list of integers and mutates it so that all duplicates are removed.
编写一个函数 remove_duplicates,它接受一个已排序的整数链表,并修改它,以便移除所有重复的元素。
def remove_duplicates(lnk):
""" Remove all duplicates in a sorted linked list.
>>> lnk = Link(1, Link(1, Link(1, Link(1, Link(5)))))
>>> Link.__init__, hold = lambda *args: print("Do not steal chicken!"), Link.__init__
>>> try:
... remove_duplicates(lnk)
... finally:
... Link.__init__ = hold
>>> lnk
Link(1, Link(5))
"""
"*** YOUR CODE HERE ***"
Hints
Hint1: Your implementation should mutate the original linked list
lnk.Hint2: DO NOT create new linked lists.
Hint3: DO NOT return the modified linked list.
Hint4: It is guaranteed that the linked list is acyclic.
你的实现应该修改原始链表 lnk。
-
不要创建新的链表。
-
不要返回修改后的链表。
-
保证该链表是不循环的。