LeetCode206. Reverse Linked List(反转链表)
题目链接:https://leetcode.com/problems/reverse-linked-list/
方法一:迭代反转
https://blog.csdn.net/qq_17550379/article/details/80647926讲的很清楚
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* pre = NULL;
ListNode* cur = head;
ListNode* lat;
while(cur){
lat = cur -> next;
cur -> next = pre;
pre = cur;
cur = lat;
}
return pre;
}
};
方法二:递归反转
解决递归问题从最简单的case入手。
(1)例如,对于链表1->2->3->4->5,
reverseList(head)返回的是5->4->3->2->1;
reverseList(head->next)返回的是5->4->3->2;(因为head->next所指代的链表是2->3->4->5->NULL)
以此类推。
(2)对于reverseList(3)这个情况,此时head为3,head->next为4,此时链表情况如下:
->->-><-
head->next->next=head这一操作后,链表变为:

然后,head->next=NULL,即
->-><-<-
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(head == NULL || head -> next == NULL)
return head;
ListNode *ans = reverseList(head -> next);
head -> next -> next = head;
head -> next = NULL;
return ans;
}
};
此外,提供一个错误写法:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(head == NULL || head -> next == NULL)
return head;
ListNode *ans = reverseList(head -> next);
ans -> next = new ListNode(head -> val);
return ans;
}
};
这种写法的错误之处在于:
以reverseList(3)为例,虽然ListNode *ans = reverseList(head -> next);返回的是5->4这个链表,不过ans指的是这个链表的第一个结点5,然后ans->next=new ListNode(head->val)后,其实在当前这一层递归中最终return的是5->3这个链表,并不是返回想象中的5->4->3这个链表。
参考:https://blog.csdn.net/qq_17550379/article/details/80647926
LeetCode206. Reverse Linked List(反转链表)的更多相关文章
- [LeetCode] 206. Reverse Linked List ☆(反转链表)
Reverse Linked List 描述 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3-> ...
- [leetcode]206. Reverse Linked List反转链表
Reverse a singly linked list. Input: 1->2->3->4->5->NULL Output: 5->4->3->2- ...
- 206 Reverse Linked List 反转链表
反转一个单链表.进阶:链表可以迭代或递归地反转.你能否两个都实现一遍?详见:https://leetcode.com/problems/reverse-linked-list/description/ ...
- 91. Reverse Linked List 反转链表
网址:https://leetcode.com/problems/reverse-linked-list/ 直接参考92:https://www.cnblogs.com/tornado549/p/10 ...
- [LeetCode] 206. Reverse Linked List 反向链表
Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. ...
- [LeetCode] Reverse Linked List 倒置链表
Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...
- Leetcode-206 Reverse Linked List
#206. Reverse Linked List Reverse a singly linked list. /** * Definition for singly-linked list. * ...
- [LintCode] Reverse Linked List 倒置链表
Reverse a linked list. Have you met this question in a real interview? Yes Example For linked list 1 ...
- LeetCode 206. Reverse Linked List倒置链表 C++
Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4-> ...
随机推荐
- VUE项目部署公网ip和端口以及使用域名访问配置
前提是已经配置好了相应的外网和内网端口的映射 一.公网ip和端口配置 在vue项目启动之前对项目下:项目名/config/index.js 文件进行修改 原来的内容为:(位置在index.js的第16 ...
- Spring 的 Bean 生命周期,11 张高清流程图及代码,深度解析
在网上已经有跟多Bean的生命周期的博客,但是很多都是基于比较老的版本了,最近吧整个流程化成了一个流程图.待会儿使用流程图,说明以及代码的形式来说明整个声明周期的流程.注意因为代码比较多,这里的流程图 ...
- twisted reactor calllater实现
twisted reactor calllater实现 1. calllater实现代码 测试源码: from twisted.internet import reactor from tw ...
- 进程作业管理2-kill,前后台作业,并行执行
kill命令:向进程发送控制信号,以实现对进程管理,每个信号对应一个数字,信号名称以SIG开 头(可省略),不区分大小写 显示当前系统可用信号: kill –l 或者 trap -l 常用信号: ...
- mark mark mark
编译并使用静态lib---->>>>转自:Walkthrough: Creating and Using a Static Library (C++) 跟踪内存分配:http: ...
- 02-13Android学习进度报告十三
今天我学习了ListView之checkbox错位问题解决.感觉还是很麻烦的. 好的存储这个Checkbox的方法有很多,你可以放到一个HashMap<Integer, Boolean>中 ...
- C语言-断言
1 作用: 断言常做语言处理的高级形式,自动处理软件隐藏很深其且它手段不易发现的错误,快速进行异常定位.同时这也是软件单元测试必须的技术. 2 使用范围: 2.1放在函数入口对入口参数进行合法性检查( ...
- Redis 简易消息队列
为了保持程序的高效,建议使用ProtoBuf. Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准,目前已经正在使用的有超过 48, ...
- jquery 判定checkbox是否选中
CheckBox 判定是否选中 使用 attr('checked')来做判别是不行的,除非所有的选中取消都是使用这个属性来处理. 正确的做法是使用 .prop('checked') 来判定.
- nginx 重写 隐藏index.php
修改 nginx.conf 文件location / { if (!-e $request_filename) { rewrite ^(.*)$ /index.php?s=$1 last; break ...