给出一个升序排序的链表,删除链表中的所有重复出现的元素,只保留原链表中只出现一次的元素。
例如:
给出的链表为, 返回.
给出的链表为, 返回.
例如:
给出的链表为, 返回.
给出的链表为, 返回.
数据范围:链表长度 ,链表中的值满足
要求:空间复杂度 ,时间复杂度
进阶:空间复杂度 ,时间复杂度
class Solution: def deleteDuplicates(self , head: ListNode) -> ListNode: # write code here x = None dummy = ListNode(0) dummy.next = head p1 = dummy p2 = head while p2 and p2.next: while p2.next and p2.val == p2.next.val: x = p2 p2.next = p2.next.next if x: p2 = p2.next p1.next = p2 x = None else: p1 = p2 p2 = p2.next return dummy.next
class Solution: def deleteDuplicates(self , head: ListNode) -> ListNode: if head is None&nbs***bsp;head.next is None: return head dummy = ListNode(-1) dummy.next, last, cur = None, None, head while cur: next_node = cur.next if next_node: if next_node.val == cur.val: val = cur.val while cur: if cur.val == val: cur = cur.next else: break else: if last is None: dummy.next = cur last = cur else: last.next = cur last = last.next cur = cur.next last.next = None else: if last is None: dummy.next = cur last = cur else: last.next = cur last = last.next cur = cur.next last.next = None return dummy.next
# class ListNode: # def __init__(self, x): # self.val = x # self.next = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param head ListNode类 # @return ListNode类 # class Solution: def deleteDuplicates(self , head: ListNode) -> ListNode: # write code here L=ListNode(None) temp=ListNode(1) L.next=temp d=dict() while head: if not d.get(head.val): d[head.val]=1 else: d[head.val]+=1 head=head.next for index,val in d.items(): if val==1: temp.next=ListNode(index) temp=temp.next return L.next.next
class Solution: def deleteDuplicates(self , head: ListNode) -> ListNode: # write code here if not head: return None if not head.next: return head dic = {} while head: if head.val in dic: dic[head.val] = 0 else: dic[head.val] = 1 head = head.next a = dic.items() res = [] if len(a)==1: return None for i,j in a: if j==0: None elif j==1: res.append(i) m = ListNode(res[0]) rt = m for i in range(1,len(res)): tmp = ListNode(res[i]) rt.next = tmp rt = tmp return m
class Solution: def deleteDuplicates(self , head: ListNode) -> ListNode: pre = ListNode(-1) prehead = pre cur = head while cur: temp = cur.val mark = 1 while cur.next and cur.val == cur.next.val: mark = 0 cur.next = cur.next.next if mark: pre.next = ListNode(cur.val) pre = pre.next cur = cur.next return prehead.next
# class ListNode: # def __init__(self, x): # self.val = x # self.next = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param head ListNode类 # @return ListNode类 # class Solution: def deleteDuplicates(self , head: ListNode) -> ListNode: # write code here ''' 升序排序的列表,记录上一个节点,比较当前节点和下一个节点值,如果一样,则当前节点下移,继续比较,直到不一样, 则上一个节点暂时指向当前节点,继续循环比较 ''' if head is None or head.next is None: return head # 首先确定新的 head。特征: front = None curNode = head nextNode = curNode.next head = None newHead = None curV = curNode.val nextV = nextNode.val # 寻找有效节点 while True: if curV != front and curV != nextV: if head is None: head = curNode newHead = head head.next = None else: head.next = curNode head = head.next head.next = None frontNode = curNode front = frontNode.val curNode = nextNode if curNode is not None: curV = curNode.val else: break if nextNode is None: nextV = None else: nextNode = nextNode.next if nextNode is not None: nextV = nextNode.val else: nextV = None return newHead
class Solution: def deleteDuplicates(self , head: ListNode) -> ListNode: if not head: return head pre = ListNode(2022) pre.next = head res = pre cur = head.next while cur: n = 0 while cur and pre.next.val == cur.val: n += 1 cur = cur.next if n == 0: pre = pre.next else: pre.next = cur if cur: cur = cur.next return res.next
############# Methond 1 ############## class ListNode: def __init__(self, x): self.val = x self.next = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param head ListNode类 # @return ListNode类 # # from collections import defaultdict class Solution: def deleteDuplicates(self , head: ListNode) -> ListNode: # 判断空链表和单值链表 if (not head)&nbs***bsp;(not head.next): return head # 通过列表生成链表 l = [] cur = head while cur: l.append(cur.val) cur = cur.next ## 初试化字典 v = 0 dic = {k:v for k in l} ## 统计数值的频次 for i in l: dic[i] += 1 ## 生成新的列表。这里已经去除了多次出现的数据 l = [] for k, v in dic.items(): if v <= 1: l.append(k) # 通过列表生成链表 res = head = ListNode(0) ## 设置头结点,省去了条件判断 for i in range(0, len(l)): res.next = ListNode(l[i]) res = res.next return head.next
class ListNode: def __init__(self, x): self.val = x self.next = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param head ListNode类 # @return ListNode类 # # from collections import defaultdict class Solution: def deleteDuplicates(self , head: ListNode) -> ListNode: # write code here # 判断空链表和单值链表 if (not head)&nbs***bsp;(not head.next): return head # 通过列表生成链表 l = [] cur = head while cur: l.append(cur.val) cur = cur.next ## 初试化 带有频次的字典 import collections dic = collections.Counter(l) ## 列表生成式 l = [k for k,v in dic.items() if v <= 1] # 通过列表生成链表 res = head = ListNode(0) ## 设置头结点,省去了条件判断 for i in range(0, len(l)): res.next = ListNode(l[i]) res = res.next return head.next
class Solution: def deleteDuplicates(self , head: ListNode) -> ListNode: if head is None: return head dummyhead = ListNode(0) dummyhead.next = head cur = dummyhead pre = cur # 记录重复元素的前一位指针 flag = 0 # 重复标志位 while cur.next : cur = cur.next # 向前走一步 while cur.next and cur.next.val == cur.val: # 判断重复 flag = 1 cur = cur.next if flag == 1: pre.next = cur.next # 去重 flag = 0 else: pre = cur return dummyhead.next
class Solution: def deleteDuplicates(self , head: ListNode) -> ListNode: # write code here if not head: return head res={} while head: if head.val not in res.keys(): res[head.val]=1 else: res[head.val]+=1 head=head.next final=[k for (k,v) in res.items() if v==1] if len(final)==0: return ListNode(1).next head=ListNode(final[0]) dummy=head for i in final[1:]: head.next=ListNode(i) head=head.next return dummy
# NC24 删除有序链表中重复的元素-II def deleteDuplicates(self, head: ListNode) -> ListNode: # 构造dummy,解决删除head问题 dummy = ListNode(-1) dummy.next = head prev, curr = dummy, head count = 0 # 重复次数 while curr: # 重复元素移动和count标记 while curr.next and curr.next.val == curr.val: count += 1 curr = curr.next if count == 0: prev = curr curr = curr.next # 重复处理,prev指向不变,变更next删除重复元素,count重置 else: prev.next = curr.next curr = curr.next count = 0 return dummy.next