[LeetCode] 206. Reverse Linked List_Easy tag: Linked List
Reverse a singly linked list.
Example:
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
Follow up:
A linked list can be reversed either iteratively or recursively. Could you implement both?
很基本的一道换顺序的题目,先建一个pre 指针,设为None,然后将head.next 存到temp node中,然后将head.next 指针指向pre,接着再将pre和head都依次后移一位。
Code:
1) Basic, using temp node
class ListNode:
def __init__(self, x):
self.val = x
self.next = None class Solution:
def reverseList(self, head):
pre = None
while head:
temp = head.next
head.next = pre
pre = head
head = temp
return pre
2) 利用python可以多重赋值
class Solution:
def reverseList(self, head):
pre = None
while head:
head.next, pre, head = pre, head, head.next
return pre
[LeetCode] 206. Reverse Linked List_Easy tag: Linked List的更多相关文章
- [LeetCode] 876. Middle of the Linked List_Easy tag: Linked List ** slow, fast pointers
Given a non-empty, singly linked list with head node head, return a middle node of linked list. If t ...
- [LeetCode] 237. Delete Node in a Linked List_Easy tag: Linked List
Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...
- leetcode 206. Reverse Linked List(剑指offer16)、
206. Reverse Linked List 之前在牛客上的写法: 错误代码: class Solution { public: ListNode* ReverseList(ListNode* p ...
- C++版 - 剑指offer 面试题16:反转链表(Leetcode 206: Reverse Linked List) 题解
面试题16:反转链表 提交网址: http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=13&tqId= ...
- LeetCode 206. Reverse Linked List (倒转链表)
Reverse a singly linked list. 题目标签:Linked List 题目给了我们一个链表,要求我们倒转链表. 利用递归,新设一个newHead = null,每一轮 把下一个 ...
- [LeetCode] 206. Reverse Linked List 反向链表
Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. ...
- LeetCode 206 Reverse a singly linked list.
Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. ...
- 迭代和递归 - leetcode 206. Reverse Linked List
Reverse Linked List,一道有趣的题目.给你一个链表,输出反向链表.因为我用的是JavaScript提交,所以链表的每个节点都是一个对象.例如1->2->3,就要得到3-& ...
- Java for LeetCode 206 Reverse Linked List
Reverse a singly linked list. 解题思路: 用Stack实现,JAVA实现如下: public ListNode reverseList(ListNode head) { ...
随机推荐
- SQL中的split方法的使用
参数说明: 1.@String :需要split的字符串 2.@Delimiter :格式化时分隔符 3.@index :返回split后数组的值 ), ),)) ) AS BEGIN )) ) DE ...
- IDEA插件开发总结
一:前置步骤 1.添加开发插件所需的SDK: 1.1先添加JDK 1.2打开Project Structure-Platform Settings-SDKs 1.3添加IntelliJ Platfor ...
- WordPaster2产品介绍
更新WordParser组件,集成ImagePaster组件功能.完善粘贴剪帖板图片,粘贴电脑图片文件,粘贴word,粘贴excel逻辑. 大幅度优化和精简js代码,js代码行数从1932行减少到97 ...
- DS8800后端的光纤通道交换式互连方式
DS8800 使用SAS 硬盘.使用了FC 到SAS 转换,光纤通道交换技术被用于DS8800 后端. FC 技术是普遍用于在一个光纤通道仲裁环路(Fibre Channel Arbitrated L ...
- Pyenv 安装部署
Pyenv Pyenv是个多版本Python管理器,可以同时管理多个Python版本共存, 区别于virtualenv. 安装 git clone git://github.com/yyuu/pyen ...
- Alpha阶段scrum meeting七天冲刺博客-天冷记得穿秋裤队
Alpha阶段scrum meeting七天冲刺博客 day url 第一天 https://www.cnblogs.com/laomiXD/articles/9874052.html 第二天 htt ...
- Android-WebView与本地HTML (Java调用--->HTML的方法)-(new WebView(this)方式)
之前的博客,Android-WebView与本地HTML (Java调用--->HTML的方法),是在 findViewById(R.id.webview);,来得到WebView, 此博客使用 ...
- [leet code 135]candy
1 题目 There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- 【推荐】Win7任务栏增强工具 7+ Taskbar Tweaker 强大的任务栏标签管理工具
我曾经推荐过一款XP的任务栏管理工具 Taskix,这是一款在XP系统中拖动任务栏内标签的小工具. XP 32位可以下载我汉化的版本 http://www.cnblogs.com/clso/archi ...
- ASP .Net C# ---CSV导入导出
CSV和Excel大致相同 复制下来 数据传到方法里面就可以了 /// <summary> /// DataTable数据写入CSV文件 /// </summary> /// ...