题目
删除链表的重复元素
1–2--2–3—3–4
return 1–2--3–4
思路
两个指针,一个cur,一个cur_next,比较cur和cur_next,若相等则cur_next后移,若不等则cur.next = cur_next.
代码
def remove_repeat(link):
head = link.head
cur = head.next
cur_next = cur.next
while cur_next is not None:
if cur.value == cur_next.value:
cur_next = cur_next.next
else:
cur.next = cur_next
cur = cur_next
cur_next = cur_next.next
cur.next = None
return head.next