[LeetCode] Reverse Lists
Well, since the head
pointer may also be modified, we create a new_head
that points to it to facilitate the reverse process.
For the example list 1 -> 2 -> 3 -> 4 -> 5
in the problem statement, it will become 0 -> 1 -> 2 -> 3 -> 4 -> 5
(we init new_head -> val
to be 0
). Then we set a pointer pre
to new_head
and another cur
to head
. Then we keep inserting cur -> next
after pre
until cur becomes the last node. The code is follows.
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* new_head = new ListNode();
new_head -> next = head;
ListNode* pre = new_head;
ListNode* cur = head;
while (cur && cur -> next) {
ListNode* temp = pre -> next;
pre -> next = cur -> next;
cur -> next = cur -> next -> next;
pre -> next -> next = temp;
}
return new_head -> next;
}
};
This link provides a more concise solution without using the new_head
. The idea is to reverse one node at a time for the beginning of the list. The rewritten code is as follows.
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* pre = NULL;
while (head) {
ListNode* next = head -> next;
head -> next = pre;
pre = head;
head = next;
}
return pre;
}
};
Well, both of the above solutions are iterative. The hint has also suggested us to use recursion. In fact, the above link has a nice recursive solution, whose rewritten code is as follows.
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;
}
};
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
.
[LeetCode] Reverse Lists的更多相关文章
- LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation
LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...
- 2016.5.16——leetcode:Reverse Bits(超详细讲解)
leetcode:Reverse Bits 本题目收获 移位(<< >>), 或(|),与(&)计算的妙用 题目: Reverse bits of a given 3 ...
- LeetCode——Reverse String
LeetCode--Reverse String Question Write a function that takes a string as input and returns the stri ...
- [LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母
Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...
- [LeetCode] Reverse String 翻转字符串
Write a function that takes a string as input and returns the string reversed. Example: Given s = &q ...
- [LeetCode] Reverse Linked List 倒置链表
Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...
- [LeetCode] Reverse Bits 翻转位
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- [LeetCode] Reverse Words in a String II 翻转字符串中的单词之二
Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...
- [LeetCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
随机推荐
- bbc mvn报错
http://www.cnblogs.com/zhouyalei/archive/2011/11/30/2268606.html
- ASP.NET MVC 简单的分页思想与实现
首先我们通过VS创建一个空的基于Razor视图引擎的ASP.NET MVC3 Web应用程序,命名为JohnConnor.Web 对创建过程或Razor不太了解的看官,请移步 ASP.NET MVC ...
- How to fix Cannot change version of project facet Dynamic Web Module to 3.0 Error in Eclipse---转载
How to fix Cannot change version of project facet Dynamic Web Module to 3.0 Error in Eclipse 原文:http ...
- Springboot接收参数
接收参数有三个方法. 1.接收id的方法: @RestController public class ControllerTest { //在这里读取配置文件 @Autowired private T ...
- 百度语音识别demo:去掉离线识别功能
如果离线识别功能不是必须的,则为了减小包体积,可按下面方法将官方demo中的离线功能去掉: 1,删除loadOfflineEngine调用.2,删除data和license文件夹. 如此可使包体积减少 ...
- Linux高性能server编程——I/O复用
IO复用 I/O复用使得程序能同一时候监听多个文件描写叙述符.通常网络程序在下列情况下须要使用I/O复用技术: client程序要同一时候处理多个socket client程序要同一时候处理用户 ...
- android实现下拉框(spinner),自己定义大小颜色背景位置,去掉默认样式黑边
1. 实现最简单的spinner xml文件,有一个TextView,一个Spinner: <RelativeLayout xmlns:android="http://schemas. ...
- socket failed:EACCES(Permission denied)
1. 权限问题 安卓端写的TCP协议软件报错原因是建立的套接字没有限权对外连接. 在AndroidManifest.xml中,加上这一句话,取得权限. <uses-permission andr ...
- hdu Distant Galaxy(遥远的银河)
Distant Galaxy Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- [python小记]使用lxml修改xml文件,并遍历目录
这次的目的是遍历目录,把目标文件及相应的目录信息更新到xml文件中.在经过痛苦的摸索之后,从python自带的ElementTree投奔向了lxml.而弃用自带的ElementTree的原因就是,na ...