【LeetCode】92. Reverse Linked List II 解题报告(Python&C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/reverse-linked-list-ii/description/
题目描述
Reverse a linked list from position m to n. Do it in one-pass.
Note: 1 ≤ m ≤ n ≤ length of list.
Example:
Input: 1->2->3->4->5->NULL, m = 2, n = 4
Output: 1->4->3->2->5->NULL
题目大意
把单链表中第m–n个元素进行翻转。
解题方法
迭代
其实就是翻转链表的而变形题目了。进行一次遍历,把第m到n个元素进行翻转,即依次插入到第m个节点的头部。
这个题还是有意思的。建议后面再多做几遍。
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
"""
count = 1
root = ListNode(0)
root.next = head
pre = root
while pre.next and count < m:
pre = pre.next
count += 1
if count < m:
return head
mNode = pre.next
curr = mNode.next
while curr and count < n:
next = curr.next
curr.next = pre.next
pre.next = curr
mNode.next = next
curr = next
count += 1
return root.next
C++代码如下:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {
int pos = 1;
ListNode* dummy = new ListNode(0);
dummy->next = head;
ListNode* pre = dummy;
ListNode* cur = head;
while (cur && pos < m) {
pre = pre->next;
cur = cur->next;
pos ++;
}
ListNode* tailNode = cur;
while (cur && pos <= n) {
ListNode* nxt = cur->next;
cur->next = pre->next;
pre->next = cur;
tailNode->next = nxt;
cur = nxt;
pos ++;
}
return dummy->next;
}
};
递归
递归解法虽然简单,但是需要对程序有深刻的认识。所以写起来并不简单。理解下面这个解法之前,最好把206. Reverse Linked List的递归解法弄懂。
首先要记住这个reverseBetween()函数的意义:翻转链表中的[m,n]区间的元素,并且返回新链表的头结点。
- 那么,如果m==n,则不用翻转,直接返回原来的头即可。
- 如果m!=1的时候,该节点不用翻转,继续翻转后面的节点。
- 如果m==1,该节点至n节点需要翻转,使用递归先把后面的翻转,此时head->next指向了翻转部分链表的结尾,把head插入到翻转部分链表的结尾即可。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {
if (m == n)
return head;
if (m != 1) {
head->next = reverseBetween(head->next, m - 1, n - 1);
return head;
} else {
ListNode* newHead = reverseBetween(head->next, 1, n - 1);
ListNode* reversedTail = head->next->next;
head->next->next = head;
head->next = reversedTail;
return newHead;
}
}
};
日期
2018 年 6 月 24 日 ———— 今天请客吃了海底捞~
【LeetCode】92. Reverse Linked List II 解题报告(Python&C++)的更多相关文章
- [LeetCode] 92. Reverse Linked List II 反向链表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 one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...
- leetcode 92 Reverse Linked List II ----- java
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倒置链表2 C++
Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...
- [leetcode]92. Reverse Linked List II反转链表2
Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...
- Leetcode#92 Reverse Linked List II
原题地址 第一步,找到将要翻转的位置,记录翻转部分前一个节点(prev) 第二步,翻转,记录翻转完成后这部分的首(reverseHead)和尾(reverseTail),以及翻转部分之后的一个节点(p ...
- [LeetCode 92] Reverse Linked List II 翻转单链表II
对于链表的问题,根据以往的经验一般都是要建一个dummy node,连上原链表的头结点,这样的话就算头结点变动了,我们还可以通过dummy->next来获得新链表的头结点.这道题的要求是只通过一 ...
- [LeetCode]92. Reverse Linked List II反转部分链表
/* 重点还是反转链表 思路就是中间的反转,然后两头接上 */ public ListNode reverseBetween(ListNode head, int m, int n) { if (he ...
- 92. Reverse Linked List II【Medium】
92. Reverse Linked List II[Medium] Reverse a linked list from position m to n. Do it in-place and in ...
随机推荐
- No.1 R语言在生物信息中的应用——序列读取及格式化输出
目的:读入序列文件(fasta格式),返回一个数据框,内容包括--存储ID.注释行(anno).长度(len).序列内容(content) 一.问题思考: 1. 如何识别注释行和序列内容行 2. 如何 ...
- char*,string,char a[], const char *,之间的转换
1. const char* 和string 转换 (1) const char*转换为 string,直接赋值即可. EX: const char* tmp = "tsinghu ...
- Dreamweaver 2019 软件安装教程
下载链接:https://www.sssam.com/1220.html#软件简介 Adobe Dreamweaver,简称"DW",DW是集网页制作和管理网站于一身的所见即所得网 ...
- PyTools-包罗万象的python工具包
PyTools-包罗万象的python工具包 <---点击这里获取代码,欢迎star. 自己平时写的代码都以函数方式封装起来了,方便代码复用. _________ ________ ______ ...
- LeetCode 从头到尾打印链表
LeetCode 从头到尾打印链表 题目描述 输入一个链表头节点,从尾到头反过来返回每个节点的值(用数组返回). 示例 1: 输入:head = [1,3,2] 输出:[2,3,1] 一得之见(Jav ...
- 日常Java 2021/9/26 (二柱升级版)
package m; import java.util.Scanner;import java.util.Random; public class di_er { static int number= ...
- Slay 全场!Erda 首次亮相 GopherChina 大会
来源|尔达 Erda 公众号 相关视频:https://www.bilibili.com/video/BV1MV411x7Gm 2021 年 6 月 26 日,GopherChina 大会准时亮相北京 ...
- aboard, abolish
aboard board做动词有上车/船/飞机的意思,boarding就是正在上.board做名词有板的意思,车厢地板的板. a是个词根,有三种意思:1. 以某种状态或方式,如: ablaze, af ...
- 2021广东工业大学十月月赛 F-hnjhd爱序列
题目:GDUTOJ | hnjhd爱序列 (gdutcode.cn) 一开始是用双指针从尾至头遍历,但发现会tle!! 后来朋友@77给出了一种用桶的做法,相当于是用空间换时间了. 其中用到的一个原理 ...
- Oracle参数文件—pfile与spfile
oracle的参数文件:pfile和spfile 1.pfile和spfile Oracle中的参数文件是一个包含一系列参数以及参数对应值的操作系统文件.它们是在数据库实例启动时候加载的, ...