leetcode刷题九
题目叙述
给定一个已排序的链表的头 head , 删除原始链表中所有重复数字的节点,只留下不同的数字 。返回 已排序的链表 。
题目解析
# 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:
head0 = head
out = []
record = None
if head == None:
return head
while head.next != None:
if head.val != head.next.val and head.val != record:
out.append(head.val)
else:
record = head.val
head = head.next
if head.val != record:
out.append(head.val)
# print(out)
head = head0
if len(out) == 0:
return None
while head.val != out[0]:
head = head.next
head0 = head
while head.next != None:
if head.next.val in out:
head = head.next
else:
head.next = head.next.next
return