206. Reverse Linked List + 92. Reverse Linked List II
▶ 关于单链表翻转的两个问题。
▶ 206. 翻转整个单链表。
● 自己的代码,9 ms,使用了递归。
class Solution
{
public:
ListNode* reverseList(ListNode* head)
{
if (head == nullptr)
return nullptr;
ListNode *p;
for (p = head; p->next != nullptr; p = p->next);// 找到末元(翻转后的首元)
reverseNode(head);
return p;
}
inline void reverseNode(ListNode* p)// 翻转以 p 指向的结点为首元的单链表,并返回一个指向末元的指针(方便下一次挂上新的首元)
{
if (p == nullptr || p->next == nullptr)
return;
reverseNode(p->next); // 翻转除首元意外的部分
p->next->next = p; // 把首元挂到最后
p->next->next->next = nullptr; // 去掉成环的部分
return ;
}
};
● 大佬的代码,9 ms,逐格移动。
class Solution
{
public:
ListNode* reverseList(ListNode* head)
{
if (head == NULL || head->next == NULL)
return head;
ListNode *prev, *cur, *temp;
for(prev = NULL, cur = head; cur != NULL;)
{
temp = cur->next;
cur->next = prev;
prev = cur;
cur = temp;
}
return prev;
}
};
▶ 92. 要求翻转单链表中第 m 到第 n(两端包含,且 m 可以等于 n)之间的所有元。
● 自己的代码,4 ms,使用了第 206 题的结果。
class Solution
{
public:
ListNode* reverseBetween(ListNode* head, int m, int n)
{
if (head == nullptr)
return head;
ListNode newHead(-), *lp, *rp, *lpprev, *temp;
newHead.next = head;
int i, j;
for (i = , lpprev = &newHead; i < m && lpprev != nullptr; i++, lpprev = lpprev->next);// 找到第 m-1 元和第 m 元
if (lpprev == nullptr || (lp = lpprev->next) == nullptr)
return head;
for (j = i, rp = lp; j < n && rp != nullptr; j++, rp = rp->next);// 找到第 n 元
if (rp == nullptr)
return head;
temp = rp->next; // 尾部不翻转部分的首元
rp->next = nullptr; // 断开翻转部分的尾部链接
lpprev->next = reverseList(lp);// 调用翻转函数
lp->next = temp; // 重新接上尾部
return newHead.next;
}
ListNode* reverseList(ListNode* head)
{
if (head == nullptr)
return nullptr;
ListNode *p;
for (p = head; p->next != nullptr; p = p->next);// 找到末元(翻转后的首元)
reverseNode(head);
return p;
}
inline void reverseNode(ListNode* p)// 翻转以 p 指向的结点为首元的单链表
{
if (p == nullptr || p->next == nullptr)
return;
reverseNode(p->next); // 翻转除首元意外的部分
p->next->next = p; // 把首元挂到最后
p->next->next->next = nullptr; // 去掉成环的部分
return;
}
};
● 大佬的代码,4 ms,也是使用逐格移动。
class Solution
{
public:
ListNode* reverseBetween(ListNode* head, int m, int n)
{
ListNode *first = new ListNode(), *t_head, *first_reverse, *node;
first->next = head;
t_head = first;
for (int i = ; i<m - ; t_head = t_head->next, i++)
first_reverse = t_head->next;
node = t_head->next;
for (int i = m; i <= n; i++)
{
ListNode * temp = node->next;
node->next = t_head->next;
t_head->next = node;
node = temp;
}
first_reverse->next = node;
return first->next;
}
};
▶ 一个副产品,交换单链表中的两个元素。我已开始把第 92 题理解错了,以为只是交换单链表中第 m 和第 n 个元素,所以写成了下面的东西。
class Solution
{
public:
ListNode* reverseBetween(ListNode* head, int m, int n)
{
if (head == nullptr)
return head;
ListNode newHead(-), *lp, *rp, *lpprev, *rpprev, *temp;
newHead.next = head;
int i, j;
for (i = , lpprev = &newHead; i < m && lpprev != nullptr; i++, lpprev = lpprev->next);
if (lpprev == nullptr || (lp = lpprev->next) == nullptr)
return head;
for (j = i, rpprev = lpprev; j < n && rpprev != nullptr; j++, rpprev = rpprev->next);
if (rpprev == nullptr || (rp = rpprev->next) == nullptr)
return head;
lpprev->next = rp;
rpprev->next = lp;
swap(lp->next, rp->next);
return newHead.next;
}
};
206. Reverse Linked List + 92. Reverse Linked List II的更多相关文章
- 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 ...
- 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- ...
- 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现
引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...
- 链表 206 Reverse Linked List, 92,86, 328, 2, 445
表不支持随机查找,通常是使用next指针进行操作. 206. 反转链表 /** * Definition for singly-linked list. * struct ListNode { * i ...
- 【Leetcode】92. Reverse Linked List II && 206. Reverse Linked List
The task is reversing a list in range m to n(92) or a whole list(206). All in one : U need three poi ...
- [LeetCode] 92. Reverse Linked List II_Medium tag: Linked List
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 倒置链表之二
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 反向链表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 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 题目地址:https://leet ...
随机推荐
- UVA-12118 Inspector's Dilemma (欧拉回路)
题目大意:一个有v个顶点的完全图,找一条经过m条指定边的最短路径. 题目分析:当每条边仅经过一次时,路径最短.给出的边可能构成若干棵树.在一棵树中,奇点个数总为偶数,若一棵树的奇点个数为0,则这棵树可 ...
- 运行MySQL远程连接
方法一:修改MySQL自带的“mysql”数据库中的“USER”表 USE mysql; UPDATE USER SET HOST='%' WHERE USER='root'; SELECT * FR ...
- FZU 2150 Fire Game 广度优先搜索,暴力 难度:0
http://acm.fzu.edu.cn/problem.php?pid=2150 注意这道题可以任选两个点作为起点,但是时间仍足以穷举两个点的所有可能 #include <cstdio> ...
- 快速切题 sgu133.Border 离线
133. Border time limit per test: 0.25 sec. memory limit per test: 4096 KB Along the border between s ...
- DIY远程监控室内温度设备(tiny6410+ds18b20+yeelink+curl)
春节了,趁着假期的空闲时间,抽空捣鼓了下tiny6410开发板,发现这个东东尘封许久,很长时间没用过了.貌似最近物联网大热,谷歌收购Nest,其设计的恒温器能够智能调节和远程控制房间的温度,UI和设计 ...
- Android Studio利用GitHub托管项目
自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...
- Map排序(按key排序,按value排序)
主要分两种,按键排序.按值排序. 而且,按key排序主要用于TreeMap,而按value排序则对于Map的子类们都适用. 一.按键排序 按Key排序主要用于TreeMap,可以实现按照Key值的大小 ...
- Windows同时安装python3和python2
Windows同时安装python3和python2 https://www.cnblogs.com/shanhua-fu/p/6912683.html Windows7 下python3和pytho ...
- 浅谈闭包(Closure)
一.闭包 好长时间对于闭包都知道与函数和其环境变量有关系,但是一直没有一个清晰的认识.今天查看了一下维基百科,顺便写下来. 二.闭包的定义 在编程语言中,闭包(语义闭包或函数闭包)是指哪些可以将语义范 ...
- python 中datetime 和 string 转换
dt = datetime.datetime.strptime(string_date, fmt) fmt 的格式说明如下: https://docs.python.org/2/library/dat ...