简体   繁体   English

如何递归地反转链表?

[英]How do I recursively reverse a linked list?

Input: 5 -> 9 -> 8 -> 3 -> 1 -> 7输入:5 -> 9 -> 8 -> 3 -> 1 -> 7

Expected Output: 7 -> 1 -> 3 -> 8 -> 9 -> 5预期 Output:7 -> 1 -> 3 -> 8 -> 9 -> 5

Issue:问题:

When I display the reversed linked list the result is 5. This is an issue because this should be the tail and not the head.当我显示反向链表时,结果为 5。这是一个问题,因为这应该是尾部而不是头部。 The rest of the nodes are missing in the display well.显示井中缺少节点的 rest。

Question:问题:

Is there an issue with the code base that is preventing the traversal from the head to the tail and changing the pointers to reverse the list?代码库是否存在阻止从头到尾遍历并更改指针以反转列表的问题?

Code:代码:

LinkedList:链表:

class LinkedList {
  constructor() {
    this.head = null;
    this.size = 0;
  }

  insertFirst(item) {
    if (this.head !== null) {
      const newHead = new _Node(item);
      let oldHead = this.head;

      oldHead.prev = newHead;
      newHead.next = oldHead;
      this.head = newHead;
    } else {
      this.head = new _Node(item, this.head);
    }

    this.size++;
  }

  insertLast(item) {
    if (!this.head) {
      this.insertFirst(item);
    } else {
      let tempNode = this.head;
      while (tempNode.next !== null) {
        tempNode = tempNode.next;
      }
      tempNode.next = new _Node(item, null, tempNode);
    }
    this.size++
  }
}

module.exports = LinkedList;

Main:主要的:

const LinkedList = require("./LinkedLists");
const { reverse } = require("./Reverse");
const { display } = require("./Supplemental");

function main() {
  let SLL = new LinkedList();

  SLL.insertFirst(5);
  SLL.insertLast(9);
  SLL.insertLast(8);
  SLL.insertLast(3);
  SLL.insertLast(1);
  SLL.insertLast(7);

  reverse(SLL);
  display(SLL);

  return SLL;
}

console.log(main());

Reverse:撤销:

reverse = (SLL) => {
  let curr = SLL.head

  if (!curr) {
    return;
  }

  if (!curr.next) {
    SLL.head = curr;
    return;
  }

  let tmp = reverse(curr.next);
  curr.next.next = curr;
  curr.next = null;
  return tmp;
}


module.exports = { reverse };

Display:展示:

display = (SLL) => {
  let currentNode = SLL.head;

  if (!SLL.head) {
    return null;
  }

  while (currentNode !== null) {
    console.log(currentNode.value);
    currentNode = currentNode.next;
  }

  return;
};

Can someone tell me what return tmp is doing in the Reverse.js file?有人能告诉我return tmp在 Reverse.js 文件中做了什么吗?

(1) Removed display from Main.js (1) 从 Main.js 中删除显示

(2) Edited Main.js: (2) 编辑 Main.js:

const LinkedList = require("./LinkedLists");
const { reverse } = require("./Reverse");
const { display } = require("./Supplemental");

function main() {
  let SLL = new LinkedList();

  SLL.insertFirst(5);
  SLL.insertLast(9);
  SLL.insertLast(8);
  SLL.insertLast(3);
  SLL.insertLast(1);
  SLL.insertLast(7);

  const result = reverse(SLL.head);
  console.log(result);

  return SLL;
}

return main();

(3) Edited Reverse.js: (3) 编辑Reverse.js:

reverse = (curr, prev = null) => {
  if (!curr) { 
    return prev;
  }

  let tmp = reverse(curr.next, curr);

  const temp = curr.next;
  curr.next = prev;
  curr.prev = temp;

  return tmp;
}

module.exports = { reverse };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM