当前位置:网站首页>[daiy4] jz76 delete duplicate nodes in the linked list (recursion)
[daiy4] jz76 delete duplicate nodes in the linked list (recursion)
2022-04-21 18:20:00 【strawberry47】
Title Description :

reflection :
The initial idea : Make one set, Save all nodes without duplication val, Finally, it is combined into a linked list . Turn around and think about it , It's troublesome to combine into a new linked list .
It would be ,phead Take a step down , If the value appears in set in , Just go down , It should be ok ~
Oh dear , It's not as simple as I thought
if pHead is None:
return None
res = before = pHead
num = set()
while pHead:
if pHead.val not in num:
before = pHead
num.add(pHead.val)
pHead = pHead.next
else:
after = pHead
while after and after.val in num:
after = after.next
else:
before.next = after
pHead = pHead.next
return res
When it comes to testing , I found that I had made a mistake ! No reservation val Duplicate values , It's all deleted ! EH , I don't know if the duplicate values are pasted together . It's a sort list , So the same values must be together . So only in When the current node is different from the next node , Before you can join .
def deleteDuplication(self , pHead: ListNode) -> ListNode:
if pHead is None:
return None
res = dummy = ListNode(0)
while pHead:
if pHead.next is None:
dummy.next = pHead
break
if pHead.val != pHead.next.val:
dummy.next = pHead
dummy = dummy.next
pHead = pHead.next
else:
repeat = pHead.val
while pHead.val == repeat:
pHead = pHead.next
if pHead is None:
dummy.next = None
break
return res.next
The recursive method
Recursion again , Every time I see recursion, I have a headache ..
- Recursive export : Consider what circumstances , We no longer need 「 Delete 」 operation . Obviously, when the parameters passed in pHead It's empty , perhaps pHead.next It's empty time , There must be no repeating elements , You can go back to pHead;
- The minimum operation of recursive link : Then consider how to delete the logic :
obviously , When pHead.val != pHead.next.val when ,pHead It can be retained , So we just need to put pHead.next Pass in recursive functions , And return the value as pHead.next, Then return pHead that will do ;
When pHead.val == pHead.next.val when ,pHead Cannot be retained , We need to use temporary variables tmp skip 「 And pHead.val A continuous segment with the same value 」, take tmp The result obtained by passing in the recursive function is returned as this time .
class Solution:
def deleteDuplication(self , pHead: ListNode) -> ListNode:
# write code here
if pHead == None or pHead.next == None:
return pHead
if pHead.val != pHead.next.val:
pHead.next = self.deleteDuplication(pHead.next)
return pHead
else:
tmp = pHead
while tmp!=None and tmp.val == pHead.val:
tmp =tmp.next
return self.deleteDuplication(tmp)
版权声明
本文为[strawberry47]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211814289886.html
边栏推荐
- Advanced formula 46 of C language: function and macro analysis
- 泛型
- 为拿几家大厂Offer,“闭关修炼
- 可爱的Tommy C语言
- 靶机渗透练习78-Thoth Tech
- How does PHP remove the first number
- 哪路神仙写的421页MySQL高级笔记,涵盖MySQL所有技术!太香了
- Lovely Tommy C language
- 干货 | 读懂 Appium 日志,让测试效率翻倍!
- [intensive reading of Thesis] perception based seam cutting for image stitching
猜你喜欢

Finally someone made it clear! It turns out that this is the global one-piece network technology with low delay

In order to offer several big factories, "closed door practice"

你必须懂也可以懂的微服务系列三:服务调用

Interface

为什么switch里的case没有break不行

MySQL - remote connection to non local MySQL database server, Error 1130: host 192.168.3.100 is not allowed to connect to this MySQL s

Just got the byte beat offer "floating"

Target penetration exercise 70-dc2

Kubernetes详解(三)——Kubernetes集群组件

终于有人讲明白了!原来这才是全球低时延一张网技术
随机推荐
Goodbye SharedPreferences, hello mmkv!
IoT平台如何实现业务配置中心
The MySQL database in MySQL is missing
干货|app自动化之如何参数化用例
终于有人讲明白了!原来这才是全球低时延一张网技术
Target penetration exercise 75-dc7
Advanced formula 46 of C language: function and macro analysis
移植openharmony之添加wifi驱动
CDF全球调查:软件交付性能停滞不前
Finally someone made it clear! It turns out that this is the global one-piece network technology with low delay
[牛客网刷题 Day4] JZ76 删除链表中重复的结点(递归)
Observe cloud landing in Alibaba cloud computing nest and build a new ISV ecosystem
Mycat水平分表(E-R表)
Easy to use example
Q: Making line chart with Excel
Target penetration exercise 70-dc2
[npj | digital medicine] machine learning of medical images: failure of methodology and suggestions for the future
The difference between integer, equals and = =
【网络】4G、5G频段汇总
干货|app自动化测试之Appium 源码修改定制分析