【LEETCODE】模拟面试-206. Reverse Linked List
图:新生大学
https://leetcode.com/problems/reverse-linked-list/
Reverse a singly linked list.
**input: **a single linked list output: a list node head of the reverse list of given input **corner: **when the list is null, or only contains one head
What we want to do is to reverse the list.
So we scan from head to tail.
For every two nodes cur
and cur.next
, we will make cur point to its forward node.
So at each step, we need a node pre
to keep connection with current scanner, so that cur.next = pre
.
And we also need a node nextOne
to memorize cur.next, since it will be changed during the scanner, if without track, cur
will lose its way to next scanner.
In order to move to next scanner, pre
will move to cur
, and cur
will move to nextOne
.
Until cur
moves to tail null, pre
is currently the new head of the reversed list, so just return it.
The idea of Recursion
is similar with Iteration
, what to do in current scanner is to keep track of nextOne
and point to pre
, and what to prepare for next step is to pass nextOne
and cur
as the new 'cur' and 'pre'.
//iteration
public class Solution{
public ListNode reverseList(ListNode head){
//corner
if ( head == null || head.next == null ){
return head;
}
ListNode pre = null;
ListNode cur = head;
while ( cur != null ){
//reverse
ListNode nextOne = cur.next;
cur.next = pre;
//prepare
pre = cur;
cur = nextOne;
}
return pre;
}
}
//recursion
public class Solution{
public ListNode reverseList(ListNode head){
//corner
if ( head == null || head.next == null ){
return head;
}
ListNode pre = null;
return helper(head, pre);
}
public ListNode helper(ListNode cur, ListNode pre){
//base
if ( cur == null ){
return pre;
}
//current
ListNode nextOne = cur.next;
cur.next = pre;
//next
return helper(nextOne, cur);
}
}
- Java基础-23(01)总结多线程,线程实现Runnable接口,线程名字获取和设置,线程控制,线程安全,同步线程
- ORA-01427问题的分析和解决(r6笔记第51天)
- 从编程实现角度学习 Faster R-CNN(附极简实现)
- Java基础-23(02)总结多线程,线程实现Runnable接口,线程名字获取和设置,线程控制,线程安全,同步线程
- 8 个最好的 Java RESTful 框架
- 【C++基础】C++11 lambda 表达式解析
- 大量redo生成的问题原因及改进(r6笔记第50天)
- Java基础-22总结登录注册IO版,数据操作流,内存操作流,打印流,标准输入输出流,
- YOLO,一种简易快捷的目标检测算法
- Java基础-22总结转换流,随机访问流,合并流,序列化流,Properties
- openshift镜像构建
- 关于数据库无法登录的问题反思(r6笔记第49天)
- SQL中distinct的用法
- 数据结构是哈希表(hashTable)(一)
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法