leetcode 206 反转链表 Reverse Linked List

C++解法一:迭代法,使用前驱指针pre,当前指针cur,临时后继指针nxt;
/**
* 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) {
ListNode* pre=NULL,*cur=head;
while(cur!=NULL){
ListNode* nxt=cur->next;
cur->next=pre;
pre=cur;cur=nxt;
}
return pre;
}
};
C++方法二:递归法,Space:O(n),Time O(n)
/**
* 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 *p=reverseList(head->next);
head->next->next=head;
head->next=NULL;
return p;
}
};
leetcode 206 反转链表 Reverse Linked List的更多相关文章
- LeetCode 206. 反转链表(Reverse Linked List) 16
206. 反转链表 206. Reverse Linked List 题目描述 反转一个单链表. 每日一算法2019/5/19Day 16LeetCode206. Reverse Linked Lis ...
- 每天一道面试题LeetCode 206 -- 反转链表
LeetCode206 反转链表 思路 代码 # # @lc app=leetcode.cn id=206 lang=python3 # # [206] 反转链表 # # https://leetco ...
- leetCode:206 反转链表
206. 反转链表 题目:反转一个单链表. 进阶:链表可以迭代或递归地反转.你能否两个都实现一遍? 非递归代码: class Solution { public ListNode reverseLis ...
- Java实现 LeetCode 206 反转链表
206. 反转链表 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL ...
- leetcode 206. 反转链表 及 92. 反转链表 II
206. 反转链表 问题描述 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1-> ...
- LeetCode 206:反转链表 Reverse Linked List
反转一个单链表. Reverse a singly linked list. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3- ...
- [Swift]LeetCode206. 反转链表 | Reverse Linked List
Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4-> ...
- 反转链表 Reverse Linked List
2018-09-11 22:58:29 一.Reverse Linked List 问题描述: 问题求解: 解法一:Iteratively,不断执行插入操作. public ListNode reve ...
- LeetCode 206.反转链表(Python3)
题目: 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL 进阶:你可 ...
随机推荐
- js 类型转变
在绝大部分情况下,操作符和函数可以自动将一个值转换成正确的数据类型.这被称为"类型转变(type conversion)". 举个例子,alert 自动转变任何类型的参数为字符串类 ...
- Go基础学习
Go基础学习 go的基础语法 fmt.Println("hello world!") //go采用行分隔符 关键字 下面列举了 Go 代码中会使用到的 25 个关键字或保留字: b ...
- 解决echarts内存泄露的问题
clear方法和dispose方法 一种是调用clear方法,一种是dispose方法.第一种是清理echarts 但是不销毁实例.第二种是销毁实例,再次使用需要重新构建实例 1. var chart ...
- 查看CPU位数的方法
查看电脑cpu的位数 WINDOWS下查看的 方法: 方法一. 在开始→运行 ...
- Malloc与Free不调用构造函数与析构函数
例子: #include "stdafx.h" #include <new> #include <iostream> using namespace std ...
- apache的rewrite机制
当我们使用thinkphp的时候,比如说我们访问一个Test控制器的test方法,http://localhost/index.php/Test/test/1.html,那个这个1是用get方式传递的 ...
- [转载](转)ISE中ROM初始化文件(.coe)的建立
原文地址:(转)ISE中ROM初始化文件(.coe)的建立作者:老徐 UltraEdit 对于ROM模块,主要是生成相应的.coe文件. 1.在Matlab中生成正余弦波形的浮点值,并量化为16bit ...
- 解决deepin没有ll等命令的办法
编辑~/.bashrc, 添加alias 如下 vim ~/.bashrc 设置别名. 添加如下行 alias ll='ls -alF' alias la='ls -A' alias vi='vim' ...
- C Make a Square Educational Codeforces Round 42 (Rated for Div. 2) (暴力枚举,字符串匹配)
C. Make a Square time limit per test2 seconds memory limit per test256 megabytes inputstandard input ...
- 线段树优化建图 || CF786B Legacy
题面:786B - Legacy 代码: #include<cstdio> #include<cstring> #include<iostream> #includ ...