当前位置:网站首页>Leetcode83 & leetcode82: delete duplicate elements in the sorting linked list
Leetcode83 & leetcode82: delete duplicate elements in the sorting linked list
2022-07-22 09:16:00 【Lemon is not sweet but sour】
Catalog
LeetCode_83: Delete duplicate elements from the sort list
LeetCode_82: Delete duplicate elements from the sort list II
LeetCode_83: Delete duplicate elements from the sort list
One 、 subject
Given a sorted linked list , Delete all duplicate elements , So that each element only appears once .
Two 、 Example
Example 1:
Input : 1->1->2
Output : 1->2
Example 2:
Input : 1->1->2->3->3
Output : 1->2->3
3、 ... and 、 Ideas
Simple violence law , The front and back pointers traverse the linked list once ,pre and cur Two hands, one before and one after .
If the values indicated by two pointers are equal , Delete cur The node pointed to by the pointer .
Four 、 Code
# 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:
"""
:param head:
:return:
"""
if not head or not head.next:
return head
pre, cur = head, head.next
while pre and cur:
if pre.val == cur.val:
pre.next = cur.next
cur = cur.next
else:
pre = pre.next
cur = cur.next
return head
if __name__ == '__main__':
head = ListNode(1)
head.next = ListNode(1)
head.next.next = ListNode(1)
head.next.next.next = ListNode(2)
head.next.next.next.next = ListNode(3)
s = Solution()
ans = s.deleteDuplicates(head)
LeetCode_82: Delete duplicate elements from the sort list II
One 、 subject
There is one. in ascending order Arranged linked list , Give you the head node of this list head , Please delete all nodes with duplicate numbers in the linked list , Keep only the original list No recurrence The number of .
Returns a list of results in ascending order .
Two 、 Example
Example 1:
Input :head = [1,2,3,3,4,4,5]
Output :[1,2,5]
Example 2:
Input :head = [1,1,1,2,3]
Output :[2,3]
Tips :
- The number of nodes in the linked list is in the range [0, 300] Inside
- -100 <= Node.val <= 100
- The title data ensures that the linked list has been arranged in ascending order
3、 ... and 、 Ideas
Simple and crude method :
First traversal , Count the numbers in the linked list , If the number of times is greater than 1, That is, the repeated numbers , Deposit it in list repeated in .
Second traversal , Is to delete duplicate numbers , If the current number is a repeating number , It will appear in list repeated in , Delete it now .
Four 、 Code
# 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):
"""
:param head: ListNode
:return: ListNode
"""
dummy = ListNode(0)
dummy.next = head
nums = dict()
node = head
# Duplicate node
repeated = []
while node:
if node.val not in nums:
nums[node.val] = 1
else:
nums[node.val] += 1
if node.val not in repeated:
repeated.append(node.val)
node = node.next
# print(nums)
# print(repeated)
# Delete operation
pre, cur = dummy, head
# pre = dummy
while cur:
if cur.val in repeated:
pre.next = cur.next
cur = cur.next
else:
pre = pre.next
cur = cur.next
return dummy.next
if __name__ == '__main__':
head = ListNode(1)
head.next = ListNode(1)
head.next.next = ListNode(1)
head.next.next.next = ListNode(2)
head.next.next.next.next = ListNode(3)
s = Solution()
ans = s.deleteDuplicates(head)
# 1->1->1->2->3
边栏推荐
猜你喜欢
Leetcode-12: integer to Roman numeral
Server memory failure prediction can actually do this!
模拟实现库函数memcpy--复制内存块。详细理解内存重叠及精准复制问题
好轮子收藏:一个支持几乎所有流行格式的图像加载库stb_image.h
【OAuth2】三、OAuth2配置解读
"Everything is interconnected, enabling thousands of industries", the 2022 open atom global open source summit openatom openharmony sub forum is about to open
Custom view - click bubble effect
How to create threads
Introduction to class loader
The pit trodden by real people tells you to avoid the 10 mistakes that novices in automated testing often make
随机推荐
前辈的前后台分离介绍
携程 Spark 多租户查询服务演进,Apache Kyuubi 未来可期
河北专接本C语言程序第一天 求1-100内的所有素数
Single cell literature learning (Part5) -- using cell to cell variability - a new era in molecular biology
浏览器获取cookie失败,浏览器解决方案
Shortcut keys for command line editing and operation skills commands
OpenGL:动态修改VBO/EBO
LeetCode342:4的幂
npm指令没有反应
模拟实现库函数memcpy--复制内存块。详细理解内存重叠及精准复制问题
A method of keeping the original scale of OpenGL map
NodeJS使用Express框架进行POST请求报“BadRequestError:request aborted”
用unshift向对象数组中添加一个元素
文献学习(part98)--Pseudo-supervised Deep Subspace Clustering
VSCODE配置Markdown,以及Markdown基础语法
【OAuth2】二、OAuth2.1的已知变动
单细胞论文记录(part5)--A joint model of unpaired data from scRNA-seq and ST for imputing missing gene ...
Analysis of encryption methods
单细胞论文记录(part17)--Integrating sc transcriptomic data across different conditions, tech and species
类加载器简介