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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. leetcode 206. Reverse Linked List(剑指offer16)、

    206. Reverse Linked List 之前在牛客上的写法: 错误代码: class Solution { public: ListNode* ReverseList(ListNode* p ...

  4. C++版 - 剑指offer 面试题16:反转链表(Leetcode 206: Reverse Linked List) 题解

    面试题16:反转链表 提交网址: http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=13&tqId= ...

  5. LeetCode 206. Reverse Linked List (倒转链表)

    Reverse a singly linked list. 题目标签:Linked List 题目给了我们一个链表,要求我们倒转链表. 利用递归,新设一个newHead = null,每一轮 把下一个 ...

  6. [LeetCode] 206. Reverse Linked List 反向链表

    Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. ...

  7. LeetCode 206 Reverse a singly linked list.

    Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. ...

  8. 迭代和递归 - leetcode 206. Reverse Linked List

    Reverse Linked List,一道有趣的题目.给你一个链表,输出反向链表.因为我用的是JavaScript提交,所以链表的每个节点都是一个对象.例如1->2->3,就要得到3-& ...

  9. Java for LeetCode 206 Reverse Linked List

    Reverse a singly linked list. 解题思路: 用Stack实现,JAVA实现如下: public ListNode reverseList(ListNode head) { ...

随机推荐

  1. pat1079+1086+1090+1094(树的遍历)感想

    今天做了这4道题,虽然大部分以前做过,但还是有些知识掌握不全. 总结一下所用的树的知识及解决方法 (1)非二叉树的遍历: 非二叉树就是图,所以它的存储结构类似邻接表,c++提供了vector数组可以很 ...

  2. Linux下启动tomcat报错,WARN org.apache.zookeeper.ClientCnxn - Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect java.net.ConnectException:

    tomcat启动完了之后,一直不停的打印这种错误信息,看表面上,应该是zk节点下的数据是空的,连接不上服务,所以一直在尝试连接,然后一直又连不上: 完整的错误信息: 407662 [usf-ZooKe ...

  3. DOM10-1节点层次

    DOM(问的那个对象模型)是针对HTML和XML文档的API.DOM描绘了一个层次化的节点树,允许开发人员添加.移除和修改页面的一部分. 每个节点都拥有各自的特点.数据和方法,另外和其他节点也存在某种 ...

  4. fPLL结构及动态配置

    输入参考时钟 从上图可以看到参考时钟输入的几种类型.   注意:fPLL的校正是由CLKUSR来驱动的,这个时钟必须要保持稳定. 参考时钟利用器     N计数器 N计数器会把参考时钟利用器输出进行分 ...

  5. DDR中的一些知识点说明(ODT,ZQ校准,OCT,TDQS)

    ODT ( On-DieTermination ,片内终结)ODT 也是 DDR2 相对于 DDR1 的关键技术突破,所谓的终结(端接),就是让信号被电路的终端吸 收掉,而不会在电路上形成反射, 造成 ...

  6. struts2访问web资源

    通过ActionContext访问 public class TestActionContextAction { public String execute(){ //获取 ActionContext ...

  7. python_day1_python简单介绍

    一.python解释器的种类 我们都知道python是一种解释型的语言,那python在执行的过程中必须要通过解释器来执行,那python的解释器到底分为哪些呢? 1.Cpython CPython是 ...

  8. Raft协议学习笔记

    目录 目录 1 1. 前言 1 2. 名词 1 3. 什么是分布式一致性? 3 4. Raft选举 3 4.1. 什么是Leader选举? 3 4.2. 选举的实现 4 4.3. Term和Lease ...

  9. switch()语句

    语法: switch(expression){ case value:statement break; case value:statement break; case value:statement ...

  10. spring配置Bean

    Bean   就是一个类 一下代码都是基于spring之IOC和DI实现案例基础上进行解析 Bean的实例化方式: 1.无参构造 <bean id="UserService" ...