题目:
编写代码,移除未排序链表中的重复节点。保留最开始出现的节点。
示例1:
输入:[1, 2, 3, 3, 2, 1]
输出:[1, 2, 3]
分析: 设置一个list用来存储未出现的节点的值,遍历待移除节点的前驱结点,如果当前节点需要被移除,则当前节点的前驱节点直接指向后继节点
class Solution:
def removeDuplicateNodes(self, head: ListNode) -> ListNode:
if head == None or head.next == None:
return head
tmp_list = list()
q = head
tmp_list.append(q.val)
while q.next != None:
cur = q.next
if cur.val not in tmp_list:
q = cur
tmp_list.append(cur.val)
else:
q.next = q.next.next
return head