python实现单链表翻转
2024-08-23 05:13:48
题目描述:
翻转一个链表
您在真实的面试中是否遇到过这个题?
Yes
样例
给出一个链表1->2->3->null,这个翻转后的链表为3->2->1->null
挑战
在原地一次翻转完成
题目分析:
在原地一次翻转完成
循环head链表,将链表中的元素从表头依次取出指向新链表即可。
源码:
"""
Definition of ListNode class ListNode(object): def __init__(self, val, next=None):
self.val = val
self.next = next
"""
class Solution:
"""
@param head: The first node of the linked list.
@return: You should return the head of the reversed linked list.
Reverse it in-place.
"""
def reverse(self, head):
# write your code here
if head is None: return None
p = head
cur = None
pre = None
while p is not None:
cur = p.next
p.next = pre
pre = p
p = cur
return pre
最新文章
- (原创)VM中的CentOS6.4中安装CloudStack6.3②
- mysql case when then end学习
- Nhiberate (一)
- delphi ftBlob二进制字段读取存储
- easy UI获取数据,打开毕弹窗
- iOS常用正则表达式验证(手机号、密码格式、身份证号等)
- HtmlAgilityPack
- Collection子接口(List/Set/Queue/SortedSet)
- 如何获取本地html文件的标题
- Linux环境编程之同步(二):条件变量
- Flex 国际化(中英语言适配)
- JavaScript的DOM编程--02--获取元素节点
- raspberry pi恢复jessie镜像之后
- Spring点滴一:Spring Ioc 容器
- Dummynet模拟高时延网络场景(Windows7)
- 简易web-slide
- 回文树(回文自动机) - BZOJ 3676 回文串
- Flume直接对接SaprkStreaming的两种方式
- Educational Codeforces Round 61 (Rated for Div. 2) D,F题解
- java io 学习笔记(三) 字符流读写
热门文章
- Working Experience - MoveWindow API 失败/无效
- 2016vr 相关白皮书
- hdu1166(线段树单点更新&;区间求和模板)
- css布局知识点汇总
- jsp学习与提高(五)——JSP 异常处理
- C# IE环境 - 重置IE(WshShell &; Rundll32)
- JSP 不同版本(转)
- CF1141F Same Sum Blocks(easy/hard)
- MySQL无法启动Couldn't find MySQL server (/usr/bin/mysqld_safe)解决办法(来源网络)
- 51nod1244 莫比乌斯函数之和 杜教筛