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 ...
随机推荐
- Vue.js图片预览插件
vue-picture-preview-extend vue-picture-preview的扩展版本,本文中插件是由其他大神开发,我做了一些扩展,原文链接:https://segmentfault. ...
- mysql插入中文数据报错 java.sql.SQLException: Incorrect string value: '\xE5\x90\x88\xE8\xAE\xA1' for column
1.我们创建数据库的时候没有更改数据库的字符集为utf8. 在mysql工具中,右击数据库,->"改变数据库",->选择“基字符集”为utf-8; 2,数据库中表的字符 ...
- IOS-更优雅地使用Static Cell
更优雅地使用Static Cell 在项目开发中,经常会用到static cell来实现一些固定的列表界面(如:个人中心等),在static cell被点击时,如何判断被点击的cell是哪一个,有什么 ...
- 【转】powerdesigner 数据类型与数据库数据类型对应
The following numeric data types are available: Standard data type DBMS-specific physical data type ...
- 16款最受关注的智能手表 苹果iWatch领衔
智能手表逐渐成为科技行业的新宠,而传闻中的苹果iWatch以及已经确认即将推出的三星Galaxy Gear,显然让这股热潮达到了顶峰,也预示着大牌厂商将逐渐进入该领域,推出更多成熟的产品.以下便是16 ...
- 常用Mysql存储引擎--InnoDB和MyISAM简单总结
常用Mysql存储引擎--InnoDB和MyISAM简单总结 2013-04-19 10:21:52| 分类: CCST|举报|字号 订阅 MySQL服务器采用了模块化风格,各部分之间保持相 ...
- ionic2中跨页面回传值
1.在跳转到新页面时传入一个contactsCallback的参数,在该参数的函数定义中做出一个承诺. 注意:最开始我本来是采用如下图方式的,但是很不幸,出现了问题,问题所在就是关于这个this的作用 ...
- 第1课 学习C++的意义
C++是C语言的加强,它们之间并不是对立的关系. 学习C++的优势: 现代软件产品的架构图: 操作系统抽象层:可有可无,但是作为一个移植性好的软件一定需要这一层.这一层的作用就是把操作系统提供的接口做 ...
- BZOJ1568: [JSOI2008]Blue Mary开公司【李超树】
Description Input 第一行 :一个整数N ,表示方案和询问的总数. 接下来N行,每行开头一个单词"Query"或"Project". 若单词为Q ...
- 新手学Appium_Python_Client
原文转自http://blog.sina.com.cn/s/blog_68f262210102v538.html 一,Appium_Python_Client的安装 推荐使用pip安装 pip ins ...