[leetcode]Reverse Linked List II @ Python
原题地址:https://oj.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.
解题思路:翻转链表的题目。
代码:
# 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 == None or head.next == None:
return head
dummy = ListNode(0); dummy.next = head
head1 = dummy
for i in range(m - 1):
head1 = head1.next
p = head1.next
for i in range(n - m):
tmp = head1.next
head1.next = p.next
p.next = p.next.next
head1.next.next = tmp
return dummy.next
[leetcode]Reverse Linked List II @ Python的更多相关文章
- 【原创】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 a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...
- [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-> ...
- [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 ...
- 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-> ...
- LeetCode Reverse Linked List II 反置链表2
题意:将指定的一段位置[m,n]的链表反置,返回链表头. 思路:主要麻烦在链表头,如果要从链表头就开始,比较特殊. 目前用DFS实现,先找到m-1的位置,再找到n+1的位置,中间这段就是否要反置的,交 ...
- 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 ...
- 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 ...
随机推荐
- 网页图表Highcharts实践教程之图表区
网页图表Highcharts实践教程之图表区 网页图表Highcharts图表区 图表区是图表的基本区域.所有的数据和图形都是绘制在图表区中.从图形绘制范围来分,图表区域分为外层图表区和绘图区.本章将 ...
- List,Set的区别
1.List,Set都是继承自Collection接口2.List特点:元素有放入顺序,元素可重复 ,Set特点:元素无放入顺序,元素不可重复(注意:元素虽然无放入顺序,但是元素在set中的位置是有该 ...
- JZYZOJ 2043 多项式除法和取余 NTT 多项式
http://172.20.6.3/Problem_Show.asp?id=2043 最开始用了FFT,交上去全tle和wa了(tle的比较多),测了一组数据发现求逆元的过程爆double了(毕竟系数 ...
- MAC下安装Homebrew 和 @权限的问题
MAC下安装Homebrew和 @权限的问题 1.Homebrew简介: Homebrew是一个包管理器,用于安装Apple没有预装但你需要的UNIX工具.(比如著名的wget). Homebrew会 ...
- Java_Certificates does not conform to algorithm constraints
java.security.cert.CertificateException: Certificates does not conform to algorithm constraints SSL证 ...
- KrakenD: API Gateway and Manager
KrakenD: API Gateway and Manager http://www.krakend.io/
- 20款最好的免费 Bootstrap 后台管理和前端模板
Admin Bootstrap Templates Free Download 1. SB Admin 2 Preview | Details & Download 2. Admin Lite ...
- 使用CefSharp在.Net程序中嵌入Chrome浏览器(四)——启动优化
在实际使用过程中,发现有的客户端会出现chrome加载网页过慢问题,定位后发现很多是因为设置系统代理所致,此时可以通过如下启动参数禁止系统代理. {"proxy-auto-detect&qu ...
- JTAG Simplified
JTAG Simplified So the other day, I explored the JTAG bus interface which is frequently found in CPL ...
- 教程:如何手动安装Xamarin与Xamarin for VisualStudio
[2016/4/17更新:如果你下载后发现仍然需要付费才能编译Android/iOS APP,请到文章最下面更新Xamarin for VS和Xamarin Studio到最新的版本.Build201 ...