题目描述:

翻转一个链表

您在真实的面试中是否遇到过这个题?

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

最新文章

  1. (原创)VM中的CentOS6.4中安装CloudStack6.3②
  2. mysql case when then end学习
  3. Nhiberate (一)
  4. delphi ftBlob二进制字段读取存储
  5. easy UI获取数据,打开毕弹窗
  6. iOS常用正则表达式验证(手机号、密码格式、身份证号等)
  7. HtmlAgilityPack
  8. Collection子接口(List/Set/Queue/SortedSet)
  9. 如何获取本地html文件的标题
  10. Linux环境编程之同步(二):条件变量
  11. Flex 国际化(中英语言适配)
  12. JavaScript的DOM编程--02--获取元素节点
  13. raspberry pi恢复jessie镜像之后
  14. Spring点滴一:Spring Ioc 容器
  15. Dummynet模拟高时延网络场景(Windows7)
  16. 简易web-slide
  17. 回文树(回文自动机) - BZOJ 3676 回文串
  18. Flume直接对接SaprkStreaming的两种方式
  19. Educational Codeforces Round 61 (Rated for Div. 2) D,F题解
  20. java io 学习笔记(三) 字符流读写

热门文章

  1. Working Experience - MoveWindow API 失败/无效
  2. 2016vr 相关白皮书
  3. hdu1166(线段树单点更新&区间求和模板)
  4. css布局知识点汇总
  5. jsp学习与提高(五)——JSP 异常处理
  6. C# IE环境 - 重置IE(WshShell & Rundll32)
  7. JSP 不同版本(转)
  8. CF1141F Same Sum Blocks(easy/hard)
  9. MySQL无法启动Couldn't find MySQL server (/usr/bin/mysqld_safe)解决办法(来源网络)
  10. 51nod1244 莫比乌斯函数之和 杜教筛