Tutorialspoint
Problem
Solution
Submissions

Reverse Linked List

Certification: Basic Level Accuracy: 0% Submissions: 0 Points: 5

Write a C program to reverse a singly linked list. Given a pointer to the head of a linked list, return a pointer to the head of the reversed list. The function should take the head of the original list as input and return the head of the reversed list.

Example 1
  • Input: 1->2->3->4->5->NULL
  • Output: 5->4->3->2->1->NULL
  • Explanation: Step 1: We start with a linked list 1->2->3->4->5->NULL. Step 2: We need to reverse the direction of each pointer. Step 3: First, 1->NULL (previous is NULL, current is 1, next is 2). Step 4: Then, 2->1->NULL (previous is 1, current is 2, next is 3). Step 5: Then, 3->2->1->NULL (previous is 2, current is 3, next is 4). Step 6: Then, 4->3->2->1->NULL (previous is 3, current is 4, next is 5). Step 7: Finally, 5->4->3->2->1->NULL (previous is 4, current is 5, next is NULL). Step 8: The new head of the reversed list is 5.
Example 2
  • Input: 1->NULL
  • Output: 1->NULL
  • Explanation: Step 1: We start with a linked list containing only one node: 1->NULL. Step 2: The reverse of a single-node list is the list itself. Step 3: So the output is also 1->NULL.
Constraints
  • The number of nodes in the list is in the range [0, 5000]
  • The values of the nodes are in the range [-5000, 5000]
  • You must reverse the list in-place with O(1) extra memory
  • Time Complexity: O(n) where n is the number of nodes in the linked list
  • Space Complexity: O(1)
Linked ListGoldman SachsZomato
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use three pointers to track the previous, current, and next nodes
  • Initialize previous as NULL, current as head
  • Iterate through the list, at each step: - Save the next node before modifying the current node - Reverse the current node's pointer to point to the previous node - Move previous and current pointers one step forward
  • After the iteration, previous will be pointing to the new head of the reversed list
  • Handle edge cases like an empty list or a list with only one node

Steps to solve by this approach:

 Step 1: Initialize three pointers: prev = NULL, current = head, next = NULL

 Step 2: Iterate through the linked list using a while loop until current becomes NULL
 Step 3: Inside the loop, first store the next node (next = current->next)
 Step 4: Reverse the current node's pointer to point to the previous node (current->next = prev)
 Step 5: Move prev and current pointers one step forward (prev = current, current = next)
 Step 6: After the loop ends, prev will be pointing to the new head of the reversed list
 Step 7: Return prev as the new head of the reversed list

Submitted Code :