Voyz's Studio.

LeetCode算法笔记--反转链表

字数统计: 100阅读时长: 1 min
2021/01/28 Share

LeetCode算法笔记-Day66

206. Reverse Linked List

Reverse a singly linked list.

1
2
3
4
5
Example:

Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL

方法一:双指针

2333.gif

Answer:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var reverseList = function(head) {
let curr = head,
pre = null;

while(curr != null){
let tmp = curr.next;
curr.next = pre;
pre = curr;
curr = tmp;
}
return pre
};
CATALOG
  1. 1. LeetCode算法笔记-Day66
    1. 1.1. 206. Reverse Linked List
    2. 1.2. 方法一:双指针
      1. 1.2.1. Answer: