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 ...
随机推荐
- 使用80percent开发rails程序:gem的了解。(kaminari)
学习目的: 对一些主要的gem进行学习了解基本功能: 作者的一些答复:(链接) 关于安全配置: 对于配置文件, 安全仅有一点: 不要提交任何敏感信息到服务端. 所以 rails-template 是添 ...
- Pave the Parallelepiped CodeForces - 1007B (计数)
大意: 给定A,B,C, 求有多少个三元组$(a,b,c)$, 满足$a \le b \le c$, 且以若干个$(a,b,c)$为三边的长方体能填满边长(A,B,C)的长方体. 暴力枚举出$A,B, ...
- maven 工程mybatis自动生成实体类
generatorConfig.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ge ...
- python运行错误------Non-UTF-8 code
1.安装-----见:https://www.cnblogs.com/weven/p/7252917.html 本文转载于:http://blog.csdn.net/youyuyixiu/articl ...
- POJ-3744-概率dp+矩阵幂(分段)
Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10214 Accepted: 2980 Desc ...
- 前端工程构建工具——Yeoman
一.Yeoman 简介 通常在开发新项目时我们都需要配置工程环境,开发目录,需要下载一些库.框架文件(如 jQuery.Backbone 等),配置编译环境(Less.Sass.Coffeescrip ...
- 20155226 2016-2017-2 《Java程序设计》第8周学习总结
20155226 2016-2017-2 <Java程序设计>第8周学习总结 教材学习内容总结 通用API 日志 java.util.logging包提供了日志功能相关类与接口,使用日志的 ...
- UVA-11478 Halum【二分】【差分约束】
LINK1 LINK2 题目大意 给你一个n个点,m条边的有向图 有一种操作把所有到达这个点的边全部减小d,把所有从从这个点出发的边加上d 问最后是否可以让所有边的边权最小值最大 如果可以无限大,输出 ...
- linux环境下maven的安装配置
1.到官网下载maven,上传到服务器上 https://maven.apache.org/download.cgi 2.将压缩包上传服务器对应路径解压: tar -zxvf apache-maven ...
- 【转】为什么要用GIT而不是SVN
原文网址:http://www.cnblogs.com/perseus/archive/2012/11/22/2782051.html 使用了近一年的git之后,再使用svn,发现自己svn这种版本管 ...