【LeetCode】206. Reverse Linked List (2 solutions)
Reverse Linked List
Reverse a singly linked list.
A linked list can be reversed either iteratively or recursively. Could you implement both?
解法一:非递归
/**
* 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;
else if(head->next->next == NULL)
{
ListNode* newhead = head->next;
newhead->next = head;
head->next = NULL;
return newhead;
}
else
{
ListNode* pre = head;
ListNode* cur = pre->next;
pre->next = NULL;
ListNode* post = cur->next; while(post != NULL)
{
cur->next = pre;
pre = cur;
cur = post;
post = post->next;
}
cur->next = pre;
return cur;
}
}
};

解法二:递归
每个节点都调到尾部去
/**
* 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* newhead = head;
while(newhead->next != NULL)
newhead = newhead->next;
reverse(head);
return newhead;
}
ListNode* reverse(ListNode* head)
{
if(head->next == NULL)
return head;
else
{
ListNode* tail = reverse(head->next);
tail->next = head;
tail = tail->next;
tail->next = NULL;
return tail;
}
}
};

【LeetCode】206. Reverse Linked List (2 solutions)的更多相关文章
- 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】206. Reverse Linked List
题目: Reverse a singly linked list. 提示: 此题不难,可以用迭代或者递归两种方法求解.记得要把原来的链表头的next置为NULL: 代码: 迭代: /** * Defi ...
- 【LeetCode】92. Reverse Linked List II 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 题目地址:https://leet ...
- 【一天一道LeetCode】#206. Reverse Linked List
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Reverse ...
- 【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
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...
- 【easy】206. Reverse Linked List 链表反转
链表反转,一发成功~ /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; ...
- 【LeetCode】234. Palindrome Linked List (2 solutions)
Palindrome Linked List Given a singly linked list, determine if it is a palindrome. Follow up:Could ...
- 【leetcode】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
随机推荐
- laravel在中间件内生成的变量如何传到控制器
在中间件内获取到一个变量,如何返回到控制器中并使用这个变量! 做了个demo: // web.php Route::get('/check', 'CheckController@check')-> ...
- how to restrict copy paste in a Textbox, in MFC?
[问题] I am developing a small application in MFC... there is a little problem..hope you guys would he ...
- ASP入门(十)-Session对象
在ASP中,有两个内部对象可以进行一些信息存储,它们是 Application 对象和 Session 对象,其中 Application 对象是对于整个应用程序期间而言的,它对于所有访问网站的用户来 ...
- linux免密码登录
ssh-copy-id 命令 可以把本地主机的公钥复制到远程主机的authorized_keys文件上,ssh-copy-id命令也会给远程主机的用户主目录(home)和~/.ssh, 和~/.ssh ...
- Android 关于操作栏 ActionBar 的设计原则【转载+整理】
原文地址 本文内容 操作栏目的 基本布局 适应旋转和不同的屏幕尺寸 副操作栏的布局 操作栏按钮 上下文操作栏 操作栏清单 设计原则就是为你在编写 Android APP 时,尤其是如何安排操作按钮的位 ...
- logstash启动脚本
1 nohup ./redis-server 1>log.log 2>error.log & 2 nohup ./elasticsearch -f & 3 nohup ...
- maven 下载源码downloadsources
mvn eclipse:eclipse -Ddownloadsources=true -Ddownloadjavadocs=true
- Windows下搭建elasticsearch集群案例
https://blog.csdn.net/u014236259/article/details/64129918
- Android Studio 之 导入Eclipse项目常见问题及解决方案
在将Eclipse做的Android项目成功导入Android Studio 后,启动生成,遇到一些问题,现总结如下: 问题1:图片命名问题 AS对图片命名要求比eclipse严格,图片名称只能有&q ...
- PAT《数据结构学习与实验指导》实验项目集 2-09 2-10 2-11 2-12 2-13
pat 2-09 装箱问题模拟 #include<cstdio> #include<set> #include<vector> using namespace st ...