leetcode python3 简单题83. Remove Duplicates from Sorted List
1.编辑器
我使用的是win10+vscode+leetcode+python3
环境配置参见我的博客:
链接
2.第八十三题
(1)题目
英文:
Given a sorted linked list, delete all duplicates such that each element appear only once.
中文:
给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list
(2)解法
① 使用双指针
(耗时:52ms,内存:13.8M)
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = Noneclass Solution:def deleteDuplicates(self, head: ListNode) -> ListNode:dummy_head = ListNode(None)dummy_head.next = headprevious = dummy_headcurrent = headwhile current:if previous and current.val == previous.val:previous.next = current.nextcurrent.next = Nonecurrent = previous.nextcontinueprevious = currentcurrent = current.nextreturn dummy_head.next
注意:
1.ListNode(0)和ListNode(None)都可以,因为反映的都是当前的表的值为空(if判断时)。
2.只有xx.next= 这样的形式才会改变xx连接的下一个表。
② 递归法,有两种
一种:
(耗时:60ms,内存:13.7M)
class Solution:def deleteDuplicates(self, head: ListNode) -> ListNode:if head is None or head.next is None:return headchild = self.deleteDuplicates(head.next)if child and head.val == child.val:head.next = child.nextchild.next = Nonereturn head
注意:
1.这里的思路是:
只要head不为空,就会进行一次递归,所以会一直递归,直到head.next为None,开始返回值,child为最后一个表,如果这个表与前一个表的值是相同的,则前一个表(head)将连接到None,也就是跳过了重复的child,再返回head,然后重复以上的返回过程即可。
2.child.next = None
其实是多余的,因为最后返回的是head,跟child没有关系的,并且去掉后,(耗时:44ms,内存:13.7M)。
另一种:
(耗时:52ms,内存:13.8M)
class Solution:def deleteDuplicates(self, head: ListNode) -> ListNode:if not head or not head.next:return headp = headwhile p and p.val == head.val:p = p.nexthead.next = self.deleteDuplicates(p)return head
注意:
1.调用递归函数的时候里面的head,p只是局部变量,不会影响上一次递归结果中的head,p。
2.or not head.next
其实是多余的,并且当执行到最后一次递归时,head=None,所以head.next是不存在的,所以会报错的,并且去掉后,(耗时:48ms,内存:13.7M)。
3.这个方法与第一种是相反的思路哦。
最后,感谢Leetcode解答区大神们的思路,这里我主要是分析code的过程。
leetcode python3 简单题83. Remove Duplicates from Sorted List相关推荐
- leetcode python3 简单题26. Remove Duplicates from Sorted Array
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二十六题 (1)题目 英文: Given a sorted array nums, ...
- leetcode python3 简单题203. Remove Linked List Elements
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百零三题 (1)题目 英文: Remove all elements from ...
- leetcode python3 简单题27. Remove Element
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二十七题 (1)题目 英文: Given an array nums and a ...
- [勇者闯LeetCode] 83. Remove Duplicates from Sorted List
[勇者闯LeetCode] 83. Remove Duplicates from Sorted List Description Given a sorted linked list, delete ...
- [leetcode]83.Remove Duplicates from Sorted List
题目 Given a sorted linked list, delete all duplicates such that each element appear only once. Exampl ...
- leetcode python3 简单题225. Implement Stack using Queues
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百二十五题 (1)题目 英文: Implement the following ...
- leetcode python3 简单题217. Contains Duplicate
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百一十七题 (1)题目 英文: Given an array of intege ...
- leetcode python3 简单题70. Climbing Stairs
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第七十题 (1)题目 英文: You are climbing a stair ca ...
- leetcode python3 简单题69. Sqrt(x)
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第六十九题 (1)题目 英文: Implement int sqrt(int x). ...
最新文章
- 股票移动平均线matlab,股票的移动平均线 (图文)
- C#读写文件:编码和转换(string和byte[]转换为例)
- JavaScript之图片操作3
- 推荐系统-应用Pandas进行数据处理
- webpack入门 --初级压缩
- 第二次北京之行-游颐和园
- 37岁程序员被裁员,面试华为阿里被拒,无奈降薪去小公司,结局出乎意料!
- 《R语言机器学习:实用案例分析》——1.3节使用函数
- 构建 Vue.js 2.0项目
- IIS上部署网站问题总结
- 容易忽视但是功能灰常强大的Java API(二. 读写对象)
- 关于同网段内主机之间通信细节
- The Second Assignment
- Mac键盘突然停止响应如何处理
- 神舟笔记本怎么进入bios?神舟笔记本bios设置U盘启动教程
- 解决 Invalid MEX-file ‘xxx.mexw64‘: 找不到指定的模块 的问题
- 一把互斥锁保护多个资源
- MySQL基础~多表查询分类与SQL92和99语法如何实现内连接和外连接
- java引用另一个程序图像,java – 在Android应用程序上显示图像的另一种方法
- 会汇编语言学c语言好学吗,只有C语言功底的我学习汇编语言可以吗?
热门文章
- 控制台没有消息循环_【干货】思科设备报错消息汇总大全~
- mac安装gnu make_Linux Make的使用以及命令安装详解
- httplistener java_Java监听器Listener使用详解
- 人脸关键点: Wing Loss for Robust Facial Landmark Localisation with Convolutional Neural Networks
- 【五级流水线CPU】—— 8. 测试结果
- Feature Scaling(特征缩放)的一些方法和使用选择
- 数字公式识别的学习笔记(legacy)
- 机器学习 Machine Learning中一元线性回归的学习笔记~
- leetcode16 3-Sum
- Altium AD20焊盘样式、热焊盘与反焊盘与直接连接