题目

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

For example:
Given 1->2->3->4->5->NULLm = 2 and n = 4,

return 1->4->3->2->5->NULL.

Note:
Given mn satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.

代码:oj测试通过 Runtime: 65 ms

 # Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
# @param head, a ListNode
# @param m, an integer
# @param n, an integer
# @return a ListNode
def reverseBetween(self, head, m, n):
if head is None or head.next is None:
return head
dummyhead = ListNode(0)
dummyhead.next = head p = dummyhead for i in range(m-1):
p = p.next curr = p.next
for i in range(n-m):
tmp = curr.next
curr.next = tmp.next
tmp.next = p.next
p.next = tmp return dummyhead.next

思路

不妨拿出四本书,摞成一摞(自上而下为 A B C D),要让这四本书的位置完全颠倒过来(即自上而下为 D C B A):

盯住书A,每次操作把A下面的那本书放到最上面

初始位置:自上而下为 A B C B

第一次操作后:自上而下为 B A C D

第二次操作后:自上而下为 C B A D

第三次操作后:自上而下为 D C B A

小白觉得四本书的例子,貌似可以更有助于解释代码,欢迎大侠拍砖指导。

leetcode 【 Reverse Linked List II 】 python 实现的更多相关文章

  1. [leetcode]Reverse Linked List II @ Python

    原题地址:https://oj.leetcode.com/problems/reverse-linked-list-ii/ 题意: Reverse a linked list from positio ...

  2. 【原创】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-p ...

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

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

  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:Given1->2 ...

  6. leetcode——Reverse Linked List II 选择链表中部分节点逆序(AC)

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

  7. LeetCode Reverse Linked List II 反置链表2

    题意:将指定的一段位置[m,n]的链表反置,返回链表头. 思路:主要麻烦在链表头,如果要从链表头就开始,比较特殊. 目前用DFS实现,先找到m-1的位置,再找到n+1的位置,中间这段就是否要反置的,交 ...

  8. lc面试准备:Reverse Linked List II

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

  9. LeetCode之“链表”:Reverse Linked List && Reverse Linked List II

    1. Reverse Linked List 题目链接 题目要求: Reverse a singly linked list. Hint: A linked list can be reversed ...

  10. LeetCode 92. 反转链表 II(Reverse Linked List II)

    92. 反转链表 II 92. Reverse Linked List II 题目描述 反转从位置 m 到 n 的链表.请使用一趟扫描完成反转. 说明: 1 ≤ m ≤ n ≤ 链表长度. LeetC ...

随机推荐

  1. Dll注入:Ring3 层 APC注入

    APC,即Asynchronous procedure call,异步程序调用APC注入的原理是:在一个进程中,当一个执行到SleepEx()或者WaitForSingleObjectEx()时,系统 ...

  2. 初学Python遇到的坑

    问题一 脚本内容 MacBookPro:Desktop mac$ cat wike.py #!/usr/bin/python from urllib.request import urlopen fr ...

  3. python_64_装饰器7

    # home密码认证是本地文件认证,bbs密码认证是远程ldat认证 import time user, passwd = 'qi', '123' def auth(auth_type): print ...

  4. python_38_try-except异常处理语句及raise的使用

    # i=10 # print(30/(i-10)) # #程序将会出现以下报错信息 # # Traceback (most recent call last): # # File "C:/U ...

  5. 解决mysql8小时无连接自动断掉机制

    windows下打开my.ini,增加: interactive_timeout=28800000 wait_timeout=28800000 MySQL是一个小型关系型数据库管理系统,由于MySQL ...

  6. 02-CSS基础与进阶-day13_2018-09-21-20-05-21

    css3动画 @keyframes 动画名 { 0% { } 100% { } } 元素执行动画 animation: 动画名 运动时间 运动曲线 01运动的汽车.html <!DOCTYPE ...

  7. axiospost请求向后端提交数据

    Axios向后端提交数据容易接收不到原因是传参方式是request payload,参数格式是json,而并非用的是form传参,所以在后台用接收form数据的方式接收参数就接收不到了.post表单请 ...

  8. JQuery根据关键字检索html元素并筛选显示

    背景:标题比较唬人,实际上就是在文本框中输入关键字,通过关键字检索html元素,筛选后显示在界面上. Html元素如下: <div> <input type="text&q ...

  9. GVIM——简直美如画,有没有!

    "========================================== " Author: wklken " Version: 9.1 " Em ...

  10. 牛客NOIP普及组R1 C括号(dp)

    题意 题目链接 Sol maya普及组的dp都要想很长时间,我真是越来越菜了qwq 设$f[i][j]$表示当前到第$i$个位置,剩下$j$个左括号没被匹配 转移的时候判断一下即可 /* */ #in ...