[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) { ...
随机推荐
- CString int转换
1.CString 转 int CString strtemp = "100"; int intResult; intResult= atoi(strtem ...
- 第01章:MongoDB简介
①MongoDB是什么 MongoDB是一个使用C++编写的.开源的.面向文档的NoSQL(Not Only SQL)数据库,也是当前最热门的NoSql数据库之一. ②MongoDB特点 1.高性能. ...
- Struts2--HelloWord
Struts2官网 官网指南 官网下载Struts2.5-min-lib.zip解压把lib目录下jar包拷贝到web项目lib目录下 配置web.xml文件 <filter> <f ...
- Matlab作图
修改线宽 plot(x,y,'LineWidth',1.5) 在xlabel,ylabel,title中使用latex语言 xlabel('$x$','interpreter','latex') yl ...
- 关于Linux学习中的问题和体会
本科期间未开展过与之相关的课程,所以初次接触Linux难免有些问题!参照老师给的学习资料中内容,逐步解决了一些问题,但还有一些问题没解决,下面列举出自己遇到的一些问题. 1.在环境变量与文件查找专题中 ...
- (动态规划)Max Sum Plus Plus--hdu--1024
http://acm.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Othe ...
- [zjoi2010]cheese
题目: 贪吃的老鼠(cheese.c/cpp/pas/in/out) 时限:每个测试点10秒 [问题描述] 奶酪店里最近出现了m只老鼠!它们的目标就是把生产出来的所有奶酪都吃掉.奶酪店中一天会生产n块 ...
- poj2478 Farey Sequence 欧拉函数的应用
仔细看看题目,按照题目要求 其实就是 求 小于等于n的 每一个数的 欧拉函数值 的总和,为什么呢,因为要构成 a/b 然后不能约分 所以 gcd(a,b)==1,所以 分母 b的 欧拉函数值 ...
- Java学习---- 数组的引用传递
1. public class ArrayRefDemo01{ public static void main(String args[]){ int temp[] = {1,3,5} ; // 利用 ...
- unidbgrid单元格操作
unidbgrid单元格操作 //GRID里回车替换TABfunction cellkeydown(sender, td, cellIndex, record, tr, rowIndex, e, eO ...