lintcode 容易题:Reverse Linked List 翻转链表
2015-10-18 16:25
381 查看
题目:
样例
给出一个链表1->2->3->null,这个翻转后的链表为3->2->1->null
挑战
在原地一次翻转完成
解题:
递归还是可以解的
java程序:
Python Code
总耗时: 223 ms
翻转链表
翻转一个链表样例
给出一个链表1->2->3->null,这个翻转后的链表为3->2->1->null
挑战
在原地一次翻转完成
解题:
递归还是可以解的
java程序:
""" 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 p = head prev = None revHead = None while p!=None: pNext = p.next if pNext ==None: revHead = p p.next = prev prev = p p = pNext return revHead
Python Code
总耗时: 223 ms
相关文章推荐
- 【C语言】 杀人凶手问题
- VS2012实现C++与Lua交互
- Android基础入门教程——10.2 SmsManager(短信管理器)
- LaTeX教程:30分钟入门
- 输入一个浮点小数, 将其按4舍5入转换成整数, 并显示
- Oracle中的length()与lengthb()的区别 ?
- MyViewPagerAdpter 和 MyFragmentPagerAdapter
- Python2.7 语言简介
- ibatis笔记小结及select @@IDENTITY as value
- storm社区相关网站和资料
- 存储过程和函数的区别
- 源代码修改错,导致ecshop后台管理员权限显示空白
- 在Eclipse添加Android兼容包( v4、v7 appcompat )
- Next Permutation
- File
- 数据结构与算法之栈
- Android Support v4、v7、v13 介绍
- php 中常用字符串
- 在cocos2d-x中使用libcurl实现上传文件功能(quick版)
- 【黑马训练营】IOS C语言通讯录