[Algorithm] Reverse a linked list

It helps to understands how recursive calls works.

function Node(val) {
  return {
    val,
    next: null
  };
}

function LinkedList() {
  return {
    head: null,
    tail: null,
    add(val) {
      const node = new Node(val);
      if (!this.head) {
        this.head = node;
        this.tail = node;
        return node;
      }

      this.tail.next = node;
      this.tail = node;
      return node;
    },
    // 1 - -2 -- x-- x
    reverse() {
      const helper = node => {
        if (!node.next) {
          this.head = node;
          return;
        }
        helper(node.next);
        // after helper call ends
        // node is three
        // node.next is four
        // swap thre and four and point three next to null
        let temp = node.next;
        temp.next = node;
        node.next = null;
      };

      return helper(this.head);
    }
  };
}

const l = new LinkedList();

l.add("one");
l.add("two");
l.add("three");
l.add("four");
l.reverse();
console.log(l.head)
// {"val":"four","next":{"val":"three","next":{"val":"two","next":{"val":"one","next":null}}}} 

So for our ‘helper‘ function, when calling it, it stop there until when reach the end.

one     |

two     |

three  |

four    |

v

helper()

four    |

three  |

tow     |

one    v

To reverse the linked list, everytime we just swap last two node, then set node.next = null

原文地址:https://www.cnblogs.com/Answer1215/p/10575678.html

时间: 2024-10-15 06:37:02

[Algorithm] Reverse a linked list的相关文章

[Algorithm] Reverse String II

给定一个字符串,要求把字符串前面的若干字符移动到字符串尾部. 解法一:蛮力法 首先想考虑将一个字符移到尾部的方法.代码如下: void LeftShiftOne(char* s, int n) { char t = s[0]; for (int i = 1; i != n; i++) s[i - 1] = s[i]; s[n - 1] = t; } 如果要移动m个字符串,则依次对函数LeftShiftOne执行m次即可.代码如下: void LeftRotateString(char* s, i

Reverse a linked list

印度哥的视频讲的很好: https://www.youtube.com/watch?v=sYcOK51hl-A

leetcode No92. Reverse Linked List II

Question: Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ 

leetcode——Reverse Linked List II 选择链表中部分节点逆序(AC)

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ le

Reverse Linked List II

题目 Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤

206. Reverse Linked List

https://leetcode.com/problems/reverse-linked-list/#/description Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. Could you implement both? Hint: The key point is to make a copy of the next link of c

92. Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the following condition:1 ≤ m ≤ n ≤ lengt

LeetCode OJ 92. Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the following condition:1 ≤ m ≤ n ≤ lengt

【链表】 Reverse Linked List II

题目: Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the following condition:1 ≤ m ≤ n ≤ l