当前位置:网站首页>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 .
边栏推荐
- MD5 encryption + salt value tool class
- You can also do NLP (classification)
- 基于深度学习的视觉目标检测技术综述
- What is the storage structure and mode of data in the database?
- OSPF综合实验
- Hierarchy selector
- I've been outsourcing for four years, but I feel it's useless
- Psychological analysis of the safest spot Silver
- [sliding window] leetcode992 Subarrays with K Different Integers
- TiDB VS MySQL
猜你喜欢

SAP ui5 application development tutorial 102 - detailed explanation of the print function of SAP ui5 applications

数据库中数据的储存结构和方式是什么?

62. different paths

cadence SPB17.4 - allegro - 優化指定單條電氣線折線連接角度 - 折線轉圓弧

How to get started with machine learning?

層次選擇器

What is the storage structure and mode of data in the database?

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

3DMAX modeling notes (I): introducing 3DMAX and creating the first model Hello World

Xiaobai operates win10 to expand Disk C (allocate disk D memory to Disk C) and the test is valid for many times
随机推荐
SAP mm me27 create intra company sto order
The longest child sequence of the 2019 Blue Bridge Cup
Have you stepped on these pits? Use caution when creating indexes on time type columns
Shell view help
leetcode 91. Decode ways (medium)
SAP ui5 application development tutorial 102 - detailed explanation of the print function of SAP ui5 applications
LINQ 查询
MySQL-Seconds_ behind_ Master accuracy error
Yyds dry goods counting tail recursion is better than recursion
通过天天基金投资基金安全吗?我打算开户买基金
Requête linq
Shell 日志与打印输出
人民币的单位的大写
[initial launch] there are too many requests at once, and the database is in danger
Because I said: volatile is a lightweight synchronized, the interviewer asked me to go back and wait for the notice!
Phantomjs Usage Summary
graphite statsd接口数据格式说明
62. 不同路径
Get the direction of mouse movement
cadence SPB17.4 - allegro - 优化指定单条电气线折线连接角度 - 折线转圆弧