难度:简单
题目描述:
思路总结:用了set以后没啥可说的了。为什么今天做两个题,因为是两道简单题,因此需要加快进度,另外,可以空出周日一天进行总结。多总结回顾也是很重要的一个环节。
PS:
题解一:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
#思路:既然这题是简单题,应该可以用set
all_vals = set()
cur = head
pre = None
while cur:
if cur.val in all_vals:
pre.next = cur.next
else:
all_vals.add(cur.val)
pre = cur #这里是一个点,如果没有删除节点才移动pre指针。
cur = cur.next
return head
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
#思路:睁大你的狗眼,是有序链表。
cur = head
while cur and cur.next:
if cur.next.val == cur.val:
cur.next = cur.next.next
else:
cur = cur.next
return head