DEV Community

ZeeshanAli-0704
ZeeshanAli-0704

Posted on

Reverse Doubly Linked List

Please consider we have doubly linked list already created...

  reverseDoublyLinkedList() {
    let current = this.head;
    let prev = null;

    if (this.head === null || this.head.next === null) {
      return this.head;
    }

    while (current) {
      prev = current.previous;
      current.previous = current.next;
      current.next = prev;
      current = current.previous;
    }
    this.head = prev.previous;
  }
Enter fullscreen mode Exit fullscreen mode

Top comments (0)