LeetCode 206. Reverse Linked List(C++)
题目:
Reverse a singly linked list.
Example:
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
Follow up:
A linked list can be reversed either iteratively or recursively. Could you implement both?
分析:
分别用迭代和递归来实现。
迭代就是新建一个newhead节点,遍历原链表,将每一个node接到newhead,注意保存head->next用来更新head。
递归则是利用函数先走到链表尾端,依次更新每一个node的next,最后返回newhead。
程序:
//iteratively
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode *newhead = NULL;
while(head){
ListNode *p = head->next;
head->next = newhead;
newhead = head;
head = p;
}
return newhead;
}
};
//recursively
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if (head == NULL || head->next == NULL){
return head;
}
ListNode* newhead = reverseList(head->next);
head->next->next = head;
head->next = NULL;
return newhead;
}
};
LeetCode 206. Reverse Linked List(C++)的更多相关文章
- leetcode 206. Reverse Linked List(剑指offer16)、
206. Reverse Linked List 之前在牛客上的写法: 错误代码: class Solution { public: ListNode* ReverseList(ListNode* p ...
- [LeetCode] 206. Reverse Linked List 反向链表
Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. ...
- 迭代和递归 - leetcode 206. Reverse Linked List
Reverse Linked List,一道有趣的题目.给你一个链表,输出反向链表.因为我用的是JavaScript提交,所以链表的每个节点都是一个对象.例如1->2->3,就要得到3-& ...
- [LeetCode] 206. Reverse Linked List ☆(反转链表)
Reverse Linked List 描述 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3-> ...
- C++版 - 剑指offer 面试题16:反转链表(Leetcode 206: Reverse Linked List) 题解
面试题16:反转链表 提交网址: http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=13&tqId= ...
- LeetCode 206. Reverse Linked List (倒转链表)
Reverse a singly linked list. 题目标签:Linked List 题目给了我们一个链表,要求我们倒转链表. 利用递归,新设一个newHead = null,每一轮 把下一个 ...
- Java for LeetCode 206 Reverse Linked List
Reverse a singly linked list. 解题思路: 用Stack实现,JAVA实现如下: public ListNode reverseList(ListNode head) { ...
- Java [Leetcode 206]Reverse Linked List
题目描述: Reverse a singly linked list. 解题思路: 使用递归或者迭代的方法. 代码如下: 方法一:递归 /** * Definition for singly-link ...
- LeetCode 206. Reverse Linked List倒置链表 C++
Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4-> ...
- LeetCode 206 Reverse Linked List 解题报告
题目要求 Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5-> ...
随机推荐
- Ionic3环境搭建及创建
初次尝试Ionic,边学习边记录下来,以免以后忘记了,入坑向( ̄ω ̄;) 1.Ionic环境安装 Ionic开发是依赖于Nodejs环境的,所以在开发之前我们需要安装好Nodejs.下载安装:http ...
- C++的一些关键字用法
const 这个关键字真是太常用了, 所以干脆总结一下. int const a = 8; //定义一个int常量a, 不能再给a赋值了 const int a = 8; //和上面一样 int co ...
- 深入了解Linux(一)
Linux的各个文件夹 每次当我使用linux的时候我都被一个个文件夹整懵逼,那么多文件夹到底是怎么分类的呢.今天终于有时间好好整理一下 /boot: 引导文件存放目录,内核文件(vmlinuz),引 ...
- 原生js三级联动
<!DOCTYPE html> <html lang="en"> <head> <title> 三级联动 </title> ...
- UCSC 工具链接
http://hgdownload.cse.ucsc.edu/admin/exe/linux.x86_64/
- js scroll nav
http://jsfiddle.net/cse_tushar/Dxtyu/141/http://ironsummitmedia.github.io/startbootstrap-scrolling-n ...
- 20155226 2016-2017-2 《Java程序设计》第4周学习总结
20155226 2016-2017-2 <Java程序设计>第4周学习总结 教材学习内容总结 继承(extends): 1.作用:提高代码复用性 让类与类产生了关系,有了这个关系才有了多 ...
- 初识Linux的感受与对它的印象——20155328张钰清
之前从未接触过虚拟机的我,由于这次寒假预备作业,稍稍地认识了一下Linux操作系统. 在自己笔记本上安装Linux操作系统 根据老师提供的<基于VirtualBox虚拟机安装Ubuntu图文教程 ...
- Android开发——你真的了解Dialog、Toast和Snackbar吗
0. 前言 今天给大家带来一篇简单易懂的关于Android提醒小功能的文章.Dialog和Toast我们都不陌生,而Snackbar是Design Support库中提供的新控件,有些朋友可能还不了解 ...
- js想不通的地方
1.js函数的function() 为什么能接受那么多参数,这些参数的名字顺序必须固定还是怎么? 怎么知道调用的时候会发送该参数过去?内部原理?手动传输? 2.js对象,json对象,java对象怎么 ...