DEV Community

kalokli8
kalokli8

Posted on

How to reverse a link list.

reverse() {
let current = this.head;
let next = null;
let previous = null;

while(current !== null) {
  next = current.next;
  current.next = previous;
  previous = current;
  current = next;
}

this.head = previous;
Enter fullscreen mode Exit fullscreen mode

}

visualization video:
https://www.youtube.com/watch?v=D7y_hoT_YZI&ab_channel=GeeksforGeeks

Top comments (0)