"""
Reverse a singly linked list.
Example:
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
""" """
两种解法
解法一:迭代法
是先申请一个pre = None 作为尾结点
cur = head 作为当前结点,end = cur.next用来存储下一个需要更新的cur的位置
让当前的cur结点与pre相连cur.next = pre,并更新pre = cur, cur = end
返回pre
""" class ListNode:
def __init__(self, x):
self.val = x
self.next = None class Solution1:
def reverseList(self, head):
pre = None
cur = head
while cur:
end = cur.next
cur.next = pre
pre = cur
cur = end
return pre """
解法二:递归
递归的思想:如果要倒转的链表有n个节点,
那么如果第一个节点后面的n-1个节点已经正确倒转了的话,
只要处理第一个和第二个节点的指向关系就可以了。
要使后面n-1个节点正确倒转,那么闲要使得后面的n-2个节点正确倒转。于是接这么递归下去。
最后只剩一个节点的时候,就什么都不用做了,只需要改变其与原来的上一个节点之间的关系就可以了。
"""
class Solution2:
def reverse(self, head):
if head.next == None:
return head
p = self.reverse(head.next)
head.next.next = head
head.next = None
return p

leetcode206 Reverse Linked List的更多相关文章

  1. Leetcode-206 Reverse Linked List

    #206.  Reverse Linked List Reverse a singly linked list. /** * Definition for singly-linked list. * ...

  2. 每天一道LeetCode--206. Reverse Linked List

    Reverse a singly linked list. package cn.magicdu; import cn.magicdu.extra.ListNode; public class _20 ...

  3. LeetCode206. Reverse Linked List(反转链表)

    题目链接:https://leetcode.com/problems/reverse-linked-list/ 方法一:迭代反转 https://blog.csdn.net/qq_17550379/a ...

  4. [LeetCode] Reverse Linked List 倒置链表

    Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...

  5. [LeetCode] Reverse Linked List II 倒置链表之二

    Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...

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

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

  7. 【leetcode】Reverse Linked List II

    Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. F ...

  8. [LintCode] Reverse Linked List 倒置链表

    Reverse a linked list. Have you met this question in a real interview? Yes Example For linked list 1 ...

  9. 14. Reverse Linked List II

    Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. F ...

随机推荐

  1. JSP页面输入框赋值换行显示问题

    <input type="hidden" id="${command.yhzlId}" value="${command.yhzx },${co ...

  2. Python学习第二十四课——Mysql 外键约束

    外键:主要是关联两个表的 举个栗子:在建表中创建外键 -- 添加外键例子 CREATE TABLE teacher( id TINYINT PRIMARY KEY auto_increment, na ...

  3. redis哨兵模式启动redis-sentinel sentinel.conf 报错

    [root@node01 redis-3.2.8]# redis-sentinel sentinel.conf *** FATAL CONFIG FILE ERROR ***Reading the c ...

  4. Git基本指令

    Git学习笔记 git //检查git是否安装 sudo apt-get install git git config --global user.name "dzq" git c ...

  5. 【转】issue management in your test project

    What is Issue Management? Issue Management is the process to make others aware of the problem and th ...

  6. C程序的执行和当前进程的结束

    内核使程序执行的唯一方法,就是调用exec函数,这个函数又会启动一个C程序启动例程,这个启动例程是C程序的启动地址.负责调用main函数,并接受mainn函数的返回值. 使得进程结束的唯一方式是隐式的 ...

  7. 吴裕雄--天生自然PythonDjangoWeb企业开发:框架基础和技术选型

    简单的Web Server import socket eol1 = b'\n\n' eol2 = b'\n\r\n' body = '''Hello,world!<h1>tszrwyx& ...

  8. js运动框架及应用

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  9. TensorFlow基础二(Shape)

    首先说明tf中tensor有两种shape,分别为static (inferred) shape和dynamic (true) shape,其中static shape用于构建图,由创建这个tenso ...

  10. Eth合约攻击续

    合同代表一个非常简单的游戏:谁给它发送了比当前奖金还大的数量的以太,就成为新的国王.在这样的事件中,被推翻的国王获得了新的奖金,但是如果你提交的话那么合约就会回退,让level重新成为国王,而我们的目 ...