【Lintcode】 035.Reverse Linked List
题目:
Reverse a linked list.
Example
For linked list 1->2->3
, the reversed linked list is 3->2->1
题解:
Solution 1 ()
class Solution {
public:
ListNode *reverse(ListNode *head) {
if (!head) {
return head;
}
ListNode *prev = nullptr;
while (head) {
ListNode *tmp = head->next;
head->next = prev;
prev = head;
head = tmp;
} return prev;
}
};
The basic idea of this recursive solution is to reverse all the following nodes after head
. Then we need to set head
to be the final node in the reversed list. We simply set its next node in the original list (head -> next
) to point to it and sets its next
to be NULL
.
Solution 2 ()
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if (!head || !(head -> next)) return head;
ListNode* node = reverseList(head -> next);
head -> next -> next = head;
head -> next = NULL;
return node;
}
};
疑问?为啥下面这段程序是错的
class Solution {
public:
ListNode *reverse(ListNode *head) {
if (!head) {
return head;
}
ListNode *prev = nullptr;
while (head) {
ListNode *tmp = head;
head->next = prev;
prev = head;
head = tmp->next; } return prev;
}
};
若一定要tmp保存head,那么程序应该如下
class Solution {
public:
ListNode *reverse(ListNode *head) {
if (!head) {
return head;
}
ListNode *prev = nullptr;
while (head) {
ListNode *tmp = new ListNode(-);
*tmp = *head;
head->next = prev;
prev = head;
head = tmp->next;
delete tmp;
} return prev;
}
};
解惑:
int main()
{
ListNode* tmp = new ListNode();
ListNode* p = new ListNode();
ListNode* q = new ListNode();
ListNode* r = new ListNode();
ListNode** t = &tmp;
tmp->next = p;
p->next = q;
q->next = r; t = &((*t)->next);
(*t) = (*t)->next; cout << (*t)->val << endl;
cout << tmp->next->val << endl;
cout << (*p).val << endl; return ;
}
输出为:2 2 1
【Lintcode】 035.Reverse Linked List的更多相关文章
- 【Lintcode】036.Reverse Linked List II
题目: Reverse a linked list from position m to n. Given m, n satisfy the following condition: 1 ≤ m ≤ ...
- 【原创】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】206. Reverse Linked List (2 solutions)
Reverse Linked List Reverse a singly linked list. click to show more hints. Hint: A linked list can ...
- 【LeetCode】92. Reverse Linked List II 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 题目地址:https://leet ...
- 【LeetCode】206. Reverse Linked List
题目: Reverse a singly linked list. 提示: 此题不难,可以用迭代或者递归两种方法求解.记得要把原来的链表头的next置为NULL: 代码: 迭代: /** * Defi ...
- 【leetcode】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-> ...
- 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...
- 【easy】206. Reverse Linked List 链表反转
链表反转,一发成功~ /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; ...
- 【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 ...
随机推荐
- caffe编译的问题 找不到opencv的 tiff库文件
解决办法: sudo su cmake .. make -j8 make pycaffe make install 问题解决. 看起来是权限问题导致.
- 软件工程第2次作业——Visual Studio 2017下基于C/C++的VSTS单元测试实践
Write one minute, test all day long. 环境确定 IDE:Microsoft Visual Studio 2017 Community 语言:C++ 单元测试工具:V ...
- ASP.NET机制详细的管道事件流程(转)
ASP.NET机制详细的管道事件流程 第一:浏览器向服务器发送请求. 1)浏览器向iis服务器发送请求网址的域名,根据http协议封装成请求报文,通过dns解析请求的ip地址,接着通过socket与i ...
- firfox浏览器常用快捷键
Ctrl + 数字键来打开第N个标签页这种还要先数完再到键盘上找数字Ctrl + Page Up = 激活左边一个标签页Ctrl + Page Down = 激活右边一个标签页Ctrl + Tab = ...
- 再说WCF Data Contract KnownTypeAttribute
WCF 中的序列化是用DataContractSerializer,所有被[DataContract]和[DataMemeber]标记的类和属性会被DataContractSerializer序列化. ...
- JQuery 获取URL中传递的参数
该方法基于JQuery,然后给方法传递URL中的参数名,返回参数值 (function($){ $.getUrlParam = function(name){ var reg = ...
- C++复习:位运算
与 a&b : 1010&1100=1000 或 a|b : 1010|1100=1110 异或 a^b : 101 ...
- go test 下篇
前言 go test 上篇 给大家介绍了golang自带的测试框架,包括单元测试和性能测试.但是在实际生产中测试经常会遇到一些网络或者依赖的第三方系统接口,运行测试用例的时候希望忽略这些接口的实际依赖 ...
- 【BZOJ2229】[Zjoi2011]最小割 最小割树
[BZOJ2229][Zjoi2011]最小割 Description 小白在图论课上学到了一个新的概念——最小割,下课后小白在笔记本上写下了如下这段话: “对于一个图,某个对图中结点的划分将图中所有 ...
- 一种微信直播H5直播与存储回放的HLS摄像机方案
接上篇 在上一篇博客<一种流量成本节省60%以上的手机直播微信直播H5直播幼儿园直播方案>中,我们一共介绍了两种省钱的HLS直播途径: 方案一:编码器或者内网推流直接对接云存储的场景 如果 ...