Reversing a linked list is a fundamental operation in computer science and programming. It involves changing the direction of the pointers in each node so that the last node becomes the first, the second-to-last node becomes the second, and so on, effectively flipping the order of the list. This operation is essential for various algorithms and data structures. Below, you'll find implementations of linked list reversal in different programming languages such as C, C++, Python, and Java.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void insert(struct Node** head_ref, int new_data) {
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList(struct Node* node) {
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
printf("
");
}
void reverse(struct Node** head_ref) {
struct Node* prev = NULL;
struct Node* current = *head_ref;
struct Node* next = NULL;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head_ref = prev;
}
int main() {
struct Node* head = NULL;
insert(&head, 1);
insert(&head, 2);
insert(&head, 3);
insert(&head, 4);
printf("Original linked list:
");
printList(head);
reverse(&head);
printf("Reversed linked list:
");
printList(head);
return 0;
}
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
};
void insert(Node** head_ref, int new_data) {
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList(Node* node) {
while (node != NULL) {
cout << node->data << " ";
node = node->next;
}
cout << endl;
}
void reverse(Node** head_ref) {
Node* prev = NULL;
Node* current = *head_ref;
Node* next = NULL;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head_ref = prev;
}
int main() {
Node* head = NULL;
insert(&head, 1);
insert(&head, 2);
insert(&head, 3);
insert(&head, 4);
cout << "Original linked list: " << endl;
printList(head);
reverse(&head);
cout << "Reversed linked list: " << endl;
printList(head);
return 0;
}
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def insert(self, new_data):
new_node = Node(new_data)
new_node.next = self.head
self.head = new_node
def printList(self):
temp = self.head
while temp:
print(temp.data, end=" ")
temp = temp.next
print()
def reverse(self):
prev = None
current = self.head
while current:
next = current.next
current.next = prev
prev = current
current = next
self.head = prev
llist = LinkedList()
llist.insert(1)
llist.insert(2)
llist.insert(3)
llist.insert(4)
print("Original linked list:")
llist.printList()
llist.reverse()
print("Reversed linked list:")
llist.printList()
class Node {
int data;
Node next;
Node(int d) {
data = d;
next = null;
}
}
class LinkedList {
Node head;
void insert(int data) {
Node new_node = new Node(data);
new_node.next = head;
head = new_node;
}
void printList() {
Node temp = head;
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
System.out.println();
}
void reverse() {
Node prev = null;
Node current = head;
Node next = null;
while (current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
head = prev;
}
public static void main(String[] args) {
LinkedList llist = new LinkedList();
llist.insert(1);
llist.insert(2);
llist.insert(3);
llist.insert(4);
System.out.println("Original linked list:");
llist.printList();
llist.reverse();
System.out.println("Reversed linked list:");
llist.printList();
}
}
Was this helpful?