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-> ...
随机推荐
- c++学习笔记(新手学习笔记,如有错误请与作者联系)
逗号”,“运算符:a = 公式1,公式2:把公式1的结果放进公式2中进行运算,如: a = 3*5 , a*4; 计算结果:a = 3*5*4=60; typedef:类型别名,为已有类型另外命名 t ...
- 【js】 Uncaught RangeError: Invalid string length
今天项目比较催的比较着急,浏览器总是崩溃,后来报了一个Uncaught RangeError: Invalid string length(字符串长度无效) 的错误. 在ajax请求后得到的json数 ...
- #leetcode刷题之路21-合并两个有序链表
将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例:输入:1->2->4, 1->3->4输出:1->1->2-&g ...
- Unbnutu下安装Apache,Mysql,php,phpmyadmin
先写一键部署脚本,肯定是先要知道如何手动安装Apache,Mysql,php,phpmyadmin 一 Apache2的安装 apt install apache2 安装好之后,手动看一下apach ...
- PhpStorm中实现代码自动换行
方法一: 随便打开一个页面,在显示行号(最左边)这里鼠标右击,勾选"Use Soft Wraps". 方法二: 选择"File-->>Settings--&g ...
- 嵌入式C语言自我修养 10:内联函数探究
10.1 属性声明:noinline & always_inline 这一节,接着讲 __atttribute__ 属性声明,__atttribute__ 可以说是 GNU C 最大的特色.我 ...
- 使用docker-compose运行微服务项目#eureka+config+auth+gateway+module
微服务架构中我们使用了必须的四个组件,eureka config gateway auth 其中config依赖eureka,auth依赖前两者,gateway又依赖auth 这样就确定了四个组件的启 ...
- docker 容器模式下部署mysql 主从复制
1.计划用两台host来部署,分别部署一台 mysql,一主一从,2.配置好主从mysql配置文件,更改文件名即可[client]port = 3306socket = /var/run/mysqld ...
- 微信小程序 request请求封装
在utils文件夹新建文件utils.js,封装代码如下: 小程序升级后内部不自带Promise方法,需外部引入Promise方法 var sendRequest = function (url, ...
- scRNA-seq genomic analysis pipline
a scRNA-seq genomic anlysis pipline .caret,.dropup>.btn>.caret{border-top-color:#000!important ...