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-> ...
随机推荐
- py基础---多线程、多进程、协程
目录 Python基础__线程.进程.协程 1.什么是线程(thread)? 2.什么是进程(process)? 3.进程和线程的区别 4.GIL全局解释器锁 5.多线程(threading模块) 6 ...
- 关于IScroll使用中的常见问题与解决方案
1.在iscroll4的滚动容器范围内,点击input框.select等表单元素时没有响应这个问题原因在于iscroll需要一直监听用户的touch操作,以便灵敏的做出对应效果,所以它把其余的默认事件 ...
- js(jQuery)tips
一:页面加上$(function(){***内容***})与不加的区别 1.这个是DOM加载完之后再加载JS代码,你的JS如果放在文档后面可能一样,但是如果你要是把JS放在head里面就有差别了(放在 ...
- thinkphp+redis实现秒杀,缓存等功能
秒杀是商城常见功能 php+redis是最常见的秒杀功能 1,安装redis,根据自己的php版本安装对应的redis扩展 首先查看phpinfo();php环境信息 2,下载redis https ...
- 2.5 USB摄像头驱动程序框架
学习目标:根据vivi驱动架构和linux-2.6.31/linux-2.6.31.14/drivers/media/video/uvc/Uvc_driver.c驱动源码,分析usb摄像头驱动程序框架 ...
- 树莓派ubuntu系统下修改config.txt文件 树莓派config.txt文件修改记录
原文:https://www.raspberrypi.org/documentation/configuration/config-txt.md译文:http://my.oschina.net/fun ...
- ubuntu18.04 无法获得锁 /var/lib/dpkg/lock - open (11: 资源暂时不可用)解决方法
出现问题: 最近打开系统之后没声儿,抽空解决以下,谁知道安装的时候出现了这个问题,一看就是锁被占了呗 直接重启大法.....不行,看来是锁分配出问题了,找了个解锁命令 jiang@ryzen:~$ s ...
- pentestbox更新msf
pentestbox成功升级msf 1. 输入 msfupdate 进行软件更新 2. 在[*] Updating gems...,软件报错,提示找不到文件路径,输入以下两条命令,尝试单独安装 g ...
- 使用HtmlAgilityPack将HtmlTable填入DataTable
HtmlAgilityPack.HtmlWeb hw = new HtmlAgilityPack.HtmlWeb(); HtmlAgilityPack.HtmlDocument doc = hw.Lo ...
- Enable CSS active pseudo styles in Mobile Safari
http://alxgbsn.co.uk/2011/10/17/enable-css-active-pseudo-styles-in-mobile-safari/ document.addEventL ...