[LeetCode]题解(python):092 Reverse Linked List II
题目来源
https://leetcode.com/problems/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->2->3->4->5->NULL, m = 2 and n = 4,
return 1->4->3->2->5->NULL.
Note:
Given m, n satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.
题意分析
Input:一个链表
Output:翻转后的链表
Conditions:给定翻转的起始位置和终止位置,翻转链表
题目思路
关键是理清变换的思路,注意在链表的操作中,经常设置一个空的头节点。
代码思路如图(注意哪些在变化,至于旋转了,程序员你懂的)

AC代码(Python)
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def reverseBetween(self, head, m, n):
"""
:type head: ListNode
:type m: int
:type n: int
:rtype: ListNode
"""
if head == None or head.next == None:
return head
newHead = ListNode(0)
newHead.next = head
head1 = newHead
for i in range(m - 1):
head1 = head1.next
p = head1.next
print p.val
for i in range(n - m):
tmp = head1.next
head1.next = p.next
p.next = p.next.next
head1.next.next = tmp
print p.val, head1.val
return newHead.next
[LeetCode]题解(python):092 Reverse Linked List II的更多相关文章
- Java for LeetCode 092 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-> ...
- LeetCode(92) 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- ...
- 092 Reverse Linked List II 反转链表 II
反转从位置 m 到 n 的链表.用一次遍历在原地完成反转.例如:给定 1->2->3->4->5->NULL, m = 2 和 n = 4,返回 1->4-> ...
- LeetCode之“链表”:Reverse Linked List && Reverse Linked List II
1. Reverse Linked List 题目链接 题目要求: Reverse a singly linked list. Hint: A linked list can be reversed ...
- LeetCode 92. 反转链表 II(Reverse Linked List II)
92. 反转链表 II 92. Reverse Linked List II 题目描述 反转从位置 m 到 n 的链表.请使用一趟扫描完成反转. 说明: 1 ≤ m ≤ n ≤ 链表长度. LeetC ...
- 【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 ...
- 【原创】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 ...
- 【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 ...
- leetcode -day30 Reverse Linked List II
1. Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one- ...
随机推荐
- lua ipairs
tbl = {"alpha", "beta", ["one"] = "uno", ["two"] = ...
- BZOJ4182 : Shopping
最后选择的一定是树上的一个连通块,考虑树分治,每次只需考虑重心必选的情况,这就变成了以重心为根的树形依赖多重背包问题. 设f[x][j]表示从根节点到x这条路径及其左边的所有节点,以及以x为根的子树的 ...
- BZOJ1397 : Ural 1486 Equal squares
二分答案mid,然后检验是否存在两个相同的mid*mid的正方形 检验方法: 首先对于每个位置,求出它开始长度为mid的横行的hash值 然后对于hash值再求一次竖列的hash值 将第二次求出的ha ...
- c/c++ 笔试面试题
#include <iostream> using namespace std; class A { public: void sayHi(){ cout<<"hel ...
- 理解cookie的path和domain属性
今天在做验证码时发现一个问题:A.B窗口都打开同一个页面,A先生成一个验证码,B再生成验证码,这时A所生成的验证码被B覆盖掉了.原因是使用了同名的cookie来存储验证码.一时找不到解决方法就参考了W ...
- codevs 1507酒厂选址
#include<cstdio> #include<cstdlib> using namespace std; int n; int dis[10010],a[10010],x ...
- TYVJ 1011 NOIP 2008&&NOIP 2000 传纸条&&方格取数 Label:多线程dp
做题记录:2016-08-15 15:47:07 背景 NOIP2008复赛提高组第三题 描述 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排做成一个m行 ...
- 手机开发必备技巧:javascript及CSS功能代码分享
1. viewport: 也就是可视区域.对于桌面浏览器,我们都很清楚viewport是什么,就是出去了所有工具栏.状态栏.滚动条等等之后用于看网页的区域,这是真正有效的区域.由于移动设备屏幕宽度不同 ...
- sql中的视图
视图可分为:普通视图和索引视图视图的作用: 1.提高数据库的安全性,降低数据库被攻击的风险,使外界无法得知数据库的数据结构 2.使查询更方便,简化程序员的操作非只读视图可以增删改,但不建议.视图的试用 ...
- android之dialog
先编写activity_main.xml文件 代码如下: <LinearLayout xmlns:android="http://schemas.android.com/apk/res ...