当前位置:网站首页>LeetCode 206. Reverse linked list (iteration + recursion)
LeetCode 206. Reverse linked list (iteration + recursion)
2022-06-23 01:11:00 【Xiaozhuang】
Hi, Hello everyone , I am Xiaozhuang .
Today's algorithm for punching cards is —— LeetCode 206. Reverse a linked list
This question will use 「 Iterative method 」 and 「 Recursive method 」 respectively
Don't talk much , Let's study together ~
One 、Leetcode subject
1、 Title address
2、 Specific topic


Two 、 Implementation code
1、 Ideas : Iterative method ;
(1) Specific code
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
/*
Ideas : Iterative method ;
Time complexity :O(n);
Spatial complexity :O(1)
*/
var reverseList = function(head) {
let pre = null;
let cur = head;
let next = head;
while(cur !== null) {
next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
return pre;
};
(2) Running results 
2、 Method : Recursive method ;
(1) Specific code
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
/*
Ideas : Recursive method to achieve ;
Time complexity :O(n);
Spatial complexity :O(n);
*/
var reverseList = function(head) {
if(head === null || head.next === null) {
return head;
}
let res = reverseList(head.next);
head.next.next = head;
head.next = null;
return res;
};
(2) Running results 
3、 The supplementary part
notes : Here is how to build a complete linked list manually , And the iterative method is used to realize .
function ListNode(val) {
this.val = val;
this.next = null;
}
function createList(n) {
let head = null;
while(n) {
let tempNode = new ListNode(n);
tempNode.next = head;
head = tempNode;
n--;
}
return head;
}
let head = createList(3);
console.log(head);//1->2->3
function getReverseList(head) {
let pre = null;
let cur = head;
let next = head;
while(cur !== null) {
next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
return pre;
}
let res = getReverseList(head);
console.log(res);//3->2->1
3、 ... and 、 Explain the video
Click to see B The station explains the video
Four 、 The supplementary part
Official account :【 Deep drift programmer Xiaozhuang 】: It contains rich learning resources and interview experience ( Not limited to the front end 、java), There are also learning exchange groups to add , In addition, there are leaders of major factories who can exchange and learn together , Progress together ~ Add Xiaozhuang wechat , reply 【 Add group 】, You can join the Internet technology exchange group .
边栏推荐
- The road of architects starts from "storage selection"
- SAP mm me27 create intra company sto order
- 使用aggregation API扩展你的kubernetes API
- 一文读懂基于Redis的Amazon MemoryDB数据库
- [22 summer reconstruction 1] codeworks round 791 (Div. 2)
- Introduction to the use of opencvsharp (C openCV) wechat QRcode decoding function (with source code attached)
- Hierarchy selector
- 启牛学堂属于证券公司吗?开户安全吗?
- Cadence spb17.4 - Chinese UI settings
- 3DMAX modeling notes (I): introducing 3DMAX and creating the first model Hello World
猜你喜欢

Read Amazon memorydb database based on redis

cadence SPB17.4 - allegro - 優化指定單條電氣線折線連接角度 - 折線轉圓弧
Yyds dry goods counting tail recursion is better than recursion

cadence SPB17.4 - 中文UI设置

How to solve the problem that easycvr does not display the interface when RTMP streaming is used?

贵金属现货白银如何呢?
![[machine learning watermelon book] update challenge [Day1]: 1.1 INTRODUCTION](/img/f6/b0df192502a59a32d8bac8c0862d02.png)
[machine learning watermelon book] update challenge [Day1]: 1.1 INTRODUCTION

New progress in the construction of meituan's Flink based real-time data warehouse platform

OSPF综合实验

層次選擇器
随机推荐
A hundred lines of code to realize reliable delay queue based on redis
Thead safety experience
層次選擇器
The road of architects starts from "storage selection"
Extend your kubernetes API using the aggregation API
[22 summer reconstruction 1] codeworks round 791 (Div. 2)
Typecho仿盧松松博客主題模板/科技資訊博客主題模板
[launch] redis Series 2: data persistence to improve availability
【机器学习-西瓜书】更文挑战【Day1】:1.1 引言
How about China International Futures Co., Ltd.? Is it a regular futures company? Is it safe to open an account online?
Hello, is the securities account presented by the Business School of qiniu business school safe? How can I open a safe stock account to speculate in stocks
LeetCode刷题——715. Range 模块
Is it reliable to get new debts? Is it safe?
Shell logs and printouts
BGP federal comprehensive experiment
Some thoughts about the technology of test / development programmers are very advanced, and they can't go on
New progress in the construction of meituan's Flink based real-time data warehouse platform
打新债到底靠不靠谱呀?是不是安全的?
Js--- SVG to png
three. JS simulated driving tour art exhibition hall - creating super camera controller