Grokking Data Structures & Algorithms for Coding Interviews
Vote

0% completed

Solution: Reverse Linked List

Problem Statement

Given the head of a singly linked list, return the head of the reversed list.

Examples

Example 1:

  • Input: [3, 5, 2]
  • Expected Output: [2, 5, 3]
  • Justification: Reversing the list [3, 5, 2] gives us [2, 5, 3].

Example 2:

  • Input: [7]
  • Expected Output: [7]
  • Justification: Since there is only one element in the list, the reversed list remains the same.

Example 3:

  • Input: [-1, 0, 1]
  • Expected Output: [1, 0, -1]
  • Justification: The list is reversed, so the elements are in the order

.....

.....

.....

Like the course? Get enrolled and start learning!
Sandeep Gattani

Sandeep Gattani

· 3 years ago

Test case seems to be incorrect. Expected result shows unreveresed linked list.

Even with the code in provided solution, the code says incorrect result upon submitting.

Show 1 reply
V

Viktor

· 3 months ago

It would be nice to stress that the task assumes reversing the linked list in-place. Otherwise, it's tempting to create a new reversed linked list, making a trivial single pass over the existing one.