leetcode python3 简单题206. Reverse Linked List
1.编辑器
我使用的是win10+vscode+leetcode+python3
环境配置参见我的博客:
链接
2.第二百零六题
(1)题目
英文:
Reverse a singly linked list.
Example:
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
中文:
反转一个单链表。
示例:
输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/reverse-linked-list
(2)解法
① 递归:先找到最后一个数3,然后依次接2,1,None就完成了
(耗时:56ms,内存:18.5M)
# 同样的代码贴上
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = Noneclass Solution:def reverseList(self, head: ListNode) -> ListNode:if not head or not head.next:return headnewH = self.reverseList(head.next)head.next.next = headhead.next = Nonereturn newH
② 迭代:就是先在表头1下面改接None,然后依次在1的前面接上2,2前面接上3就完成了
(耗时:48ms,内存:14.5M)
# 同样经典代码贴上
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = Noneclass Solution:def reverseList(self, head: ListNode) -> ListNode:cur, pre = head, Nonewhile cur:cur.next, pre, cur = pre, cur, cur.nextreturn pre
注意:
1.cur.next, pre, cur = pre, cur, cur.next
与下面的代码是不等价的!
cur.next = prepre = curcur = cur.next
要等价,可以等效为:
temp = cur.nextcur.next = prepre = curcur = temp
因为多变量的赋值是会创建临时变量空间来存放的,所以不会只要是使用相同的变量名称,值都不会变!
2如果要本地测试解法②,直接运行下面的代码即可
class ListNode:def __init__(self, x):self.val = xself.next = Noneclass SingleLinkList:def __init__(self, node=None):self.__head = nodedef is_Empty(self):return self.__head is Nonedef append(self, item):node = ListNode(item)if self.is_Empty():self.__head = nodeelse:cur = self.__headwhile cur.next is not None: cur = cur.nextcur.next = nodedef find_head(self):return self.__headclass Solution:def reverseList(self, head: ListNode) -> ListNode:cur, pre = head, Nonewhile cur:cur.next, pre, cur = pre, cur, cur.nextreturn prels = SingleLinkList()
data = [1, 2, 3]
for i in data:ls.append(i)
print(type(ls))
mm = Solution()
res = mm.reverseList(ls.find_head())while res:print(res.val, end='')res = res.next
3.补充一点
a=1
b=2
c=3
a, b, c = b, a+b, b
#输出
a=2
b=1+2=3
c=b=2
leetcode python3 简单题206. Reverse Linked List相关推荐
- leetcode python3 简单题234. Palindrome Linked List
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百三十四题 (1)题目 英文: Given a singly linked li ...
- leetcode python3 简单题203. Remove Linked List Elements
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百零三题 (1)题目 英文: Remove all elements from ...
- leetcode python3 简单题190. Reverse Bits
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百九十题 (1)题目 英文: Reverse bits of a given 3 ...
- leetcode python3 简单题7.Reverse integer
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第七题 (1)题目 英文: Given a 32-bit signed intege ...
- leetcode python3 简单题160. Intersection of Two Linked Lists
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百六十题 (1)题目 英文: Write a program to find t ...
- leetcode python3 简单题141. Linked List Cycle
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百四十一题 (1)题目 英文: Given a linked list, det ...
- leetcode python3 简单题21. Merge Two Sorted Lists(Linked)
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二十一题 (1)题目 英文: Merge two sorted linked li ...
- leetcode python3 简单题225. Implement Stack using Queues
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百二十五题 (1)题目 英文: Implement the following ...
- leetcode python3 简单题83. Remove Duplicates from Sorted List
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第八十三题 (1)题目 英文: Given a sorted linked list ...
最新文章
- 在Ubuntu中打开pycharm步骤:
- linux mint开启热点,Linux Mint 17 + 小米WIFI创建手机热点
- Java创建MySQL句柄_MySQL创建用户(CREATE USER)
- Java进阶:Semaphore信号量基本使用
- html显示任务进度,(原创)asp.net利用多线程执行长时间的任务,客户端显示出任务的执行进度的示例(二)...
- h5列表 php,常用的HTML5列表标签
- 软件架构的数据流总结(三)
- jzoj3682-Points and Segments【模型转化,欧拉回路】
- C语言:fopen与open的总结
- linux安装jdk(以1.6为例)
- Android 开发之旅:深入分析布局文件又是“Hello World!”
- 谷歌AI魔镜:看你手舞足蹈,就召唤出8万幅照片学你跳 | TensorFlow.js
- 计算器排html页面,简易计算器html页面代码
- 数据结构面试常见问题总结
- Mybatis 自学笔记【全结尾狂神说练习29道】
- linux系统怎样进入图形界面,Linux系统中如何切换图形界面与字符界面
- AOSP ~ Camera - RK HAL3 ( 一 )
- 软件开发中,站立会议的必要性
- 上海疫情后一个前端的面试心路历程
- 赛普拉斯PSoC6正式接入阿里云Link TEE加强物联网应用的安全设计...
热门文章
- geoserver发布瓦片数据_geoserver地图发布服务教程(3)——快速配置矢量样式
- 余弦函数导数推导过程_反三角函数的导数的推导过程
- 机器学习算法_机器学习之EM算法和概率图模型
- c语言程序设计单元小测,C语言程序设计单元小测2.doc
- Junit使用方法总结
- algorithm头文件下的reverse()
- 【less-7】sqli-labs靶场第七关(类似less-5)
- Java中final、finally、finalize的理解
- QTextEdit显示中文乱码解决,中文GB2312转Unicode,QString、QByteArray 转换,16进制显示,toUtf8与toLocal8Bit区别
- 数据分析5大关键环节