当前位置:网站首页>Leetcode 83. delete duplicate elements in the sorting linked list
Leetcode 83. delete duplicate elements in the sorting linked list
2022-07-29 06:23:00 【Zhangchuming ZCM】
Power button | 83. Delete duplicate elements from the sort list
Title screenshot

Method 1 : One traverse ( Single pointer )
Because the linked list has been sorted , It can be traversed at one time , Directly remove each element of the linked list from the previous elements .
utilize cur The pointer points to the head node , Then start traversing , If cur And cur.next The corresponding elements are the same , let cur.next Point to cur.next.next, You can delete this node . After traversal, return to the header node .
class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
if not head:
return head
cur = head
while cur.next:
if cur.val == cur.next.val:
cur.next = cur.next.next
else:
cur = cur.next
return headComplete test 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:
if not head:
return head
cur = head
while cur.next:
if cur.val == cur.next.val:
cur.next = cur.next.next
else:
cur = cur.next
return head
class Linked_list:
def create_linked_list(self, nums):
if len(nums) == 0:
return None
head = ListNode(nums[0])
cur = head
for i in range(1, len(nums)):
cur.next = ListNode(nums[i])
cur = cur.next
return head
def print_linked_list(self, list_node):
if list_node is None:
return
cur = list_node
while cur:
print(cur.val, '->', end=' ')
cur = cur.next
print('null')
class main:
a = Solution()
list_1 = [1, 1, 2, 3, 3]
ll = Linked_list()
h = ll.create_linked_list(list_1)
b = a.deleteDuplicates(h)
ll.print_linked_list(b)
if __name__ == '__main__':
main()Method 2 : recursive
Regard the linked list as a head node with a short linked list hanging behind it . The linked list can be seen as a head node hanging another shorter linked list , And so on .
Deal with it in the following way :
- Delete a short list All repetitions The elements of ;
- Judge whether the value of the head node of the original linked list is equal to that of the first 1 The value of the shorter chain header node after step processing ;
- If equal , Then return a shorter linked list ;
- otherwise , Hang a short linked list behind the head node of the original linked list , Back again .
for example :1->2->2->3->3->4->5
From back to front
The last linked list is 5
take 5 Hang on 4 On :4->5
No repetition , Hang it directly on 3 On :3->4->5
345 No repetition , Then hang it in the front 3 On :3->3->4->5
Two 3 repeated , After deletion :4->5
3 and 4 It's not equal , Hang the new short linked list on the original head node 3 On :3->4->5
At this time “3” It's the second to last “3”, The last one “3” It has been deleted .
And so on , You can delete the penultimate “2” obtain :2->3->4->5
Finally get :1->2->3->4->5
class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
if not head or not head.next: return head
head.next = self.deleteDuplicates(head.next)
return head.next if head.val == head.next.val else headMethod 3 : Double pointer ( Speed pointer )
Use two pointers to control .
The condition is set to the head node and the next one of the head nodes is non empty . If there is no linked list or the linked list is directly an element , Then return directly .
When there are more than two elements : The linked list uses two pointers to judge , If the element value pointed by the slow pointer and the fast pointer are different , Then the speed pointer continues to point to the next node , If the same , The slow pointer points to the next node of the fast pointer , That is, the node currently pointed to by the fast pointer is deleted . Then the fast pointer continues to move by itself , repeat , Until the fast pointer points to the end of the linked list .
class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
if head is None or head.next is None:
return head
slow = head
fast = head.next
while fast:
if slow.val != fast.val:
slow = slow.next
else:
slow.next = fast.next
fast = fast.next
return headMethod four : Set dummy node
The previous method is to judge the special situation ( The linked list is empty or has only one node ), It can be eliminated by setting dummy nodes .
That is to add a virtual head node in front of the original linked list .
class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
dummyHead = ListNode(1000, head)
cur = dummyHead
while cur.next:
if cur.next.val == cur.val:
cur.next = cur.next.next
else:
cur = cur.next
return dummyHead.next边栏推荐
- 【软件工程之美 - 专栏笔记】26 | 持续交付:如何做到随时发布新版本到生产环境?
- markdown与Typora
- LeetCode #7.整数反转
- 【软件工程之美 - 专栏笔记】23 | 架构师:不想当架构师的程序员不是好程序员
- 动态规划总结
- 无符号右移
- Mathematical modeling experience
- Operating system interview questions
- UE5 landscape 换算 Nanite 转换方式及不支持 配合 Lumen及Lumen开启 Dynamic Mesh 使用方法
- leetcode刷题笔记 605. Can Place Flowers (Easy) 605.种花问题
猜你喜欢

FPGA based: moving target detection (supplementary simulation results, available)

Pit avoidance: about the interconnection of two hc-05 master-slave integrated Bluetooth modules, there is no connection problem

FPGA based: multi-target motion detection (hand-in-hand teaching ①)

SQLyog 安装和配置教程

Leetcode 167. sum of two numbers II - input ordered array
![[beauty of software engineering - column notes] 13 | how to break the rhythm of writing code during daytime meetings and overtime?](/img/e2/56234084d0cfad6906f9e84212182a.png)
[beauty of software engineering - column notes] 13 | how to break the rhythm of writing code during daytime meetings and overtime?

HR must ask questions - how to fight with HR (collected from FPGA Explorer)

LeetCode #557.反转字符串中的单词 III

leetcode刷题笔记 452. Minimum Number of Arrows to Burst Balloons (Medium) 452.用最少数量的箭引爆气球(中等)
![寒假集训总结 (1.23~1.28) [第一梯队]](/img/cf/2f86ecc23bfe6d96ad0429c785663a.png)
寒假集训总结 (1.23~1.28) [第一梯队]
随机推荐
crawl笔记
Leetcode 26. delete duplicates in the ordered array
Markdown and typora
Joiner.on和stream().map联合使用技巧
LeetCode #876.链表的中间结点
Eight sorts --------- quick sort
网络安全学习篇
Eight sorts ----------- bubble sort
Mathematical modeling experience
SimpleFOC+PlatformIO踩坑之路
Jingwei Qili: OLED character display based on hmep060 (and Fuxi project establishment demonstration)
MySql-面试题
HR must ask questions - how to fight with HR (collected from FPGA Explorer)
官方教程 Redshift 08 Light
Install MySQL from scratch (MySQL installation document - unzipped version)
【软件工程之美 - 专栏笔记】26 | 持续交付:如何做到随时发布新版本到生产环境?
【软件工程之美 - 专栏笔记】25 | 有哪些方法可以提高开发效率?
Sqlyog installation and configuration tutorial
从头安装MYSQL(MYSQL安装文档-解压版)
FPGA based: multi-target motion detection (hand-in-hand teaching ①)