Reverse a Linked List Coding Interview Question |
Interview Question

Reverse a Linked List

Course launching October 2020

Follow me on YouTube for free coding interview videos.

Users who sign up for the email list will receive an exclusive 75% discount at launch.

Your subscription could not be saved. Please try again.
Your subscription was successful! 🤓

Write a function reverseLinkedList that takes a head node of a singly linked lists, reverses it in place, and returns the new head node.

You will be given the head as the input, and you should return the new head for the reverse linked list which is the current tail node. All the items should be in reverse order, starting with the tail (new head). The revere should happen in place which means you should not create a new linked list.

class Node {
  constructor(data) { = data; = null;

// Input
const head = new Node(42);
42 -> 52 -> 4 -> 6 -> 1 -> 9000

// Output
9000 -> 1 -> 6 -> 4 -> 52 -> 42

Validate My Answer

  1. You only need to iterate through the list once which means it will be O(n) time.

  2. You will need three pointers to stores nodes and no additional data structures. This will be O(1) space.

  3. The solution doesn't require a lot of code, but it does require doing the procedure carefully and in the in the right order. Make sure you handle this correctly and think through edge cases.