当前位置:网站首页>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 .
边栏推荐
- Xiaobai operates win10 to expand Disk C (allocate disk D memory to Disk C) and the test is valid for many times
- Vector 6 (inheritance)
- Js--- SVG to png
- 基于深度学习的视觉目标检测技术综述
- Graphite statsd interface data format description
- How do beginners get started quickly and learn deeply?
- How to solve the problem that easycvr does not display the interface when RTMP streaming is used?
- Tidb monitoring upgrade: a long way to solve panic
- Template specialization template <>
- 打新债属于什么理财产品?
猜你喜欢

Wallys/DR7915-wifi6-MT7915-MT7975-2T2R-support-OpenWRT-802.11AX-supporting-MiniPCIe

LeetCode 206. 反转链表(迭代+递归)

崔鹏团队:万字长文梳理「稳定学习」全景图

【机器学习-西瓜书】更文挑战【Day1】:1.1 引言

A hundred lines of code to realize reliable delay queue based on redis

SAP ui5 application development tutorial 102 - detailed trial version of print function implementation of SAP ui5 application

Dig three feet to solve the data consistency problem between redis and MySQL

Read Amazon memorydb database based on redis

黄金etf持仓量如何算

Quelle est la structure et la façon dont les données sont stockées dans la base de données?
随机推荐
Const defined variables and for of and for in in JS
層次選擇器
cadence SPB17.4 - 中文UI设置
Which platform is safer to buy stocks on?
Some thoughts about the technology of test / development programmers are very advanced, and they can't go on
LINQ query
手机上券商开户哪个券商平台更好更安全,如果需要佣金低的怎么办
Is it safe to open a new bond? How
What financial product does the new bond belong to?
Installation record of ros1noetic in Win 11
数据库中数据的储存结构和方式是什么?
E-R图
3DMAX modeling notes (I): introducing 3DMAX and creating the first model Hello World
C#.NET万能数据库访问封装类(ACCESS、SQLServer、Oracle)
How to solve the problem that easycvr does not display the interface when RTMP streaming is used?
three. JS simulated driving tour art exhibition hall - creating super camera controller
How to get started with machine learning?
Introduction to the use of opencvsharp (C openCV) wechat QRcode decoding function (with source code attached)
Flink synchronizes MySQL data to es
Add expiration time for localstorage