---
title: "Write a C program to reverse a linked list in place, without using any extra memory."  
description: "Write a C program to reverse a linked list in place, without using any extra memory."  
author: "Utpal Vishwas"  
published: 2023-04-21  
updated: 2023-04-22  
canonical: https://www.mindstick.com/forum/157956/write-a-c-program-to-reverse-a-linked-list-in-place-without-using-any-extra-memory  
category: "C Language"  
tags: ["data structure", "programs"]  
reading_time: 2 minutes  

---

# Write a C program to reverse a linked list in place, without using any extra memory.

Write a C [program to reverse](https://www.mindstick.com/forum/157954/write-a-c-program-to-reverse-a-given-string-without-using-any-built-in-functions) a [linked](https://answers.mindstick.com/qa/104773/how-to-create-a-linked-list) list in place, without using any [extra](https://www.mindstick.com/blog/63570/5-ways-to-make-money-with-the-extra-space-in-your-home) memory.

## Replies

### Reply by Aryan Kumar

```c
#include <stdio.h>
#include <stdlib.h>
// Define the linked list node
struct ListNode {
   int val;
   struct ListNode* next;
};
// Function to create a new linked list node
struct ListNode* newNode(int data) {
   struct ListNode* node = (struct ListNode*) malloc(sizeof(struct ListNode));
   node->val = data;
   node->next = NULL;
   return node;
}
// Function to reverse a linked list in place
struct ListNode* reverseList(struct ListNode* head) {
   struct ListNode *prev = NULL, *curr = head, *next = NULL;
   while (curr != NULL) {
       next = curr->next;
       curr->next = prev;
       prev = curr;
       curr = next;
   }
   return prev;
}
int main() {
   struct ListNode* head = newNode(1);
   head->next = newNode(2);
   head->next->next = newNode(3);
   head->next->next->next = newNode(4);
   head->next->next->next->next = newNode(5);
   printf("Original List: ");
   struct ListNode* node = head;
   while (node != NULL) {
       printf("%d ", node->val);
       node = node->next;
   }
   printf("\n");
   head = reverseList(head);
   printf("Reversed List: ");
   node = head;
   while (node != NULL) {
       printf("%d ", node->val);
       node = node->next;
   }
   printf("\n");
   return 0;
}
```

In this [program](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-program), we define the linked list node structure and a function to create a new linked list node. We then define a function **reverseList** that takes the head of a linked list as an argument and returns a pointer to the head of the reversed linked list. The function uses three pointers, **prev**, **curr**, and **next**, to keep track of the previous, current, and next nodes in the original linked list. Initially, **prev** is NULL, **curr** is the head of the linked list, and **next** is NULL. We traverse the linked list using a while loop and for each node, we set **next** to the next node, set the **curr->next** pointer to **prev**, set **prev** to **curr**, and set **curr** to **next**. Finally, we return **prev**, which is the head of the reversed linked list.

In the **main** function, we create a linked list and print the original list. We then call the **reverseList** function to [reverse](https://www.mindstick.com/blog/63740/what-are-the-most-effective-and-safest-thanks-to-reverse-erectile-dysfunction) the linked list in place and print the reversed list.


---

Original Source: https://www.mindstick.com/forum/157956/write-a-c-program-to-reverse-a-linked-list-in-place-without-using-any-extra-memory

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
