零 标题:算法(leetcode,附思维导图 + 全部解法)300题之(206)反转链表
一 题目描述
二 解法总览(思维导图)
三 全部解法
1 方案1
1)代码:
var reverseList = function(head) {
if (!head) {
return null;
}
let resList = [],
resHead = head;
while(head){
resList.push(head.val);
head = head.next;
}
const l = resList.length;
head = resHead;
index = l - 1;
while (index >= 0) {
head.val = resList[index];
index--;
head = head.next;
}
return resHead;
};
2 方案2
1)代码:
var reverseList = function(head) {
if (head === null || head.next === null) {
return head;
}
const resHead = reverseList(head.next);
head.next.next = head;
head.next = null;
return resHead;
}
3 方案3
1)代码:
var reverseList = function(head) {
let pre = null,
cur = head;
while (cur) {
const next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
return pre;
}
四 资源分享 & 更多
1 历史文章 - 总览
2 博主简介
码农三少 ,一个致力于编写 极简、但齐全题解(算法) 的博主。 专注于 一题多解、结构化思维 ,欢迎一起刷穿 LeetCode ~
|