【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 ...
随机推荐
- 分词中的HMM
http://blog.csdn.net/heavendai/article/details/7030102 1. 首先来说一下马尔科夫链. 一个事件序列发生的概率可以用下面的概率论里 ...
- neo4j的配置文件(图文详解)
不多说,直接上干货! 前期博客 Ubuntu16.04下Neo4j图数据库官网安装部署步骤(图文详解)(博主推荐) Ubuntu14.04下Neo4j图数据库官网安装部署步骤(图文详解)(博主推荐) ...
- 在centos7.4上安装mysql5.5
from: https://www.digitalocean.com/community/tutorials/how-to-install-mysql-on-centos-7
- MAC升级nodejs和npm到最新版
第一步,先查看本机node.js版本: node -v 第二步,清除node.js的cache: sudo npm cache clean -f 第三步,安装 n 工具,这个工具是专门用来管理node ...
- Spring4新特性——集成Bean Validation 1.1(JSR-349)到SpringMVC
在之前的<跟我学SpringMVC>中的<第七章 注解式控制器的数据验证.类型转换及格式化>中已经介绍过SpringMVC集成Bean Validation 1.0(JSR-3 ...
- iOS开发技巧 - 使用Alerts和Action Sheets显示弹出框
解决方案: (Swift) 使用UIAlertController类 (Objective-C) 使用UIAlertView类 代码: (Swift) import UIKit class ViewC ...
- jQuery clearQueue
clearQueue()方法与clearQueue()方法结合: .clearQueue()可用于删除通过.queue()方法添加到通用jQuery序列的任何函数. 示例: <!DOCTYPE ...
- 深度学习-Caffe中启用MatlabSupport编译出错的解决方案
一.如果编译前打算生成支持Matlab的库,则设置MatlabSupport为true之后. 二.记得添加Matlab的安装路径.我的是:D:\Application\DevTools\Matlab ...
- hive中简单介绍分区表(partition table)——动态分区(dynamic partition)、静态分区(static partition)
一.基本概念 hive中分区表分为:范围分区.列表分区.hash分区.混合分区等. 分区列:分区列不是表中的一个实际的字段,而是一个或者多个伪列.翻译一下是:“在表的数据文件中实际上并不保存分区列的信 ...
- Java 时区之间时间转换
SimpleDateFormat foo = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); System.out.println(&qu ...