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. vue 开发系列(五) 调用原生API

    概要 我们在开发手机端程序的时候了,我们经常需要使用到拍照,二维码的功能.数字天堂公司提供了大量的原生API支持. http://www.html5plus.org/doc/ 实现 1.在hbuild ...

  2. acitvemq的jvm监控

    在conf/activemq.xml启用jmx监控 即在broker后面添加useJmx="true"配置参数 <managementContext> <mana ...

  3. mysql重装之后 复制data

    (哇,编程小白的第一篇博客丫,激动) Q one:mysql需要重装,数据该怎么办. 方法一:数据表最好是导出成.sql文件,这样才比较安全. 方法二:直接copy了data文件:在mysql安装盘下 ...

  4. ubuntu-server-12.04.2安装配置jdk

    原文链接:http://blog.csdn.net/amymengfan/article/details/9958461 我选择的是离线安装,这需要先下载好jdk安装包(下载地址:http://www ...

  5. 2.2.11同步synchronized方法无限等待与解决

    同步方法容易造成死循环. package com.cky.bean; /** * Created by edison on 2017/12/8. */ public class Service { s ...

  6. MIT Molecular Biology 笔记2 DNA的突变和修复

    视频  https://www.bilibili.com/video/av7973580?from=search&seid=16993146754254492690 教材 Molecular ...

  7. 20155326 第12周课堂实践总结(二)String类和Arrays类的学习

    20155326 第12周课堂实践总结(二)String类和Arrays类的学习 实践二 Arrays和String单元测试 实践题目 在IDEA中以TDD的方式对String类和Arrays类进行学 ...

  8. codeforce868c

    C. Qualification Rounds time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  9. VirtualBox 安装 CentOS6.5 教程

    VirtualBox安装 CentOS6.5教程 1.选择第一个进行安装 2.选择右面的 skip 进入安装 3.点击next 4.选择中文简体 5.选择美式键盘 6.选择第一个 7.输入主机名 8. ...

  10. java基础知识-原码,反码,补码

    1.正数:原码,反码,补码:都一样. 2.负数:和正数的储存方式不同,负数都是以补码形式存储的. <1>负数的补码 把负数的原码除了符号位取反后再+1. <2>负数的原码 把对 ...