InternDoor

Turn a linked list around

Easylinked list · pointers

Reported in interviews at (where)

Reverse the singly linked list that starts at head, so that its last node comes first and every next pointer points the other way. Return the new head.

An empty list stays empty. Try it in one pass with O(1) extra memory.

Example 1
Input
head = [4,8,15]
Output
[15,8,4]

The node holding 4 ends up last, pointing to nothing.

Example 2
Input
head = [7]
Output
[7]

One node is its own reverse.

Example 3
Input
head = []
Output
[]

An empty list has nothing to turn around.

Limits

  • 0 <= number of nodes <= 100000
  • -100000 <= Node.val <= 100000

Hints

Hint 1

Walk the list keeping three pointers: the node before, the current node and the one after it.

Hint 2

Point the current node back at the one before, then step all three forward. The last node you reach is the new head.

Where this was reported

Our own problem on the task these candidates describe; the wording, examples and tests are InternDoor’s.

Run your code to check it on the examples. Submit runs every hidden test.

Ready for the real one?

Engineering internships and entry-level jobs in India, newest first.