🗂️
Data Structures
  • Data Structures Manual
  • Arrays
    • Array ADT
    • Linear Search
    • Binary Search
    • Some More Basic Operations
    • Reversing an Array
    • Operations in a Sorted Array
    • Merging Two Arrays
    • Set Operations
    • Finding Missing Elements
    • Duplicates in an Array
    • Getting a Pair whose Sum = K
    • Finding Max & Min in Single Scan
  • Strings
    • Finding the Length of a String
    • Changing Cases in a String
    • Finding Number of Vowels, Consonants & Words
    • Reversing a String
    • Checking for Palindrome
    • Duplicates in a String
    • Checking if Strings are Anagrams
    • Permutations of a String
  • Singly Linked List
    • Displaying the Nodes
    • Counting the Nodes
    • Sum of all Nodes
    • Finding the Maximum Element
    • Searching in a Node
    • Inserting a Node
    • Inserting a Node in Sorted List
    • Deleting a Node
    • Checking if List is Sorted
    • Removing Duplicates from a List
    • Reversing a Linked List
    • Concatenating Two Lists
    • Detecting a Loop in Linked List
    • Merge Two Sorted Lists
    • Finding the Middle Node
  • Cirular Linked List
    • Displaying the Nodes
    • Inserting a Node
    • Deleting a Node
  • Doubly Linked List
    • Inserting a Node
    • Deleting a Node
    • Reversing a Doubly Linked List
    • Circular Doubly Linked List
  • Stack
    • Stack Using Array
    • Stack Using Linked List
    • Balancing Parenthesis
    • Infix to Postfix
    • Evaluation of Postfix Expression
  • Queue
    • Queue using Array
    • Queue using Linked List
    • Double Ended Queue
  • Binary Tree
    • Creating a Binary Tree using Queue
    • Recursive Tree Traversals
    • Iterative Tree Traversals
    • Level Order Traversal
    • Counting Nodes in a Binary Tree
    • Finding the Height of Tree
    • Finding Sum of All Nodes
  • Binary Search Tree
    • Searching in a BST
    • Inserting in a BST
    • Deleting in a BST
  • AVL Tree
    • Inserting in an AVL Tree
    • AVL Tree Rotations
    • Deleting in an AVL Tree
  • Heap
    • Inserting in a Heap
    • Deleting in a Heap
    • Heapify
Powered by GitBook
On this page
  • Reversing by Changing Node Data & Using an Array :
  • Reversing using Sliding Pointers :
  • Reversing by Recursion :

Was this helpful?

  1. Singly Linked List

Reversing a Linked List

Iterative and recursive approaches to reverse a linked list.

Reversing by Changing Node Data & Using an Array :

  • Pass the pointer to head node and an array as parameters.

  • Using a while loop, copy the data from each node to the array until the pointer becomes NULL.

  • Set the pointer to head and decrement to index i by 1 to reach the last element of array.

  • Overwrite the data in each node using a while loop and keep decrementing the index i until pointer becomes NULL.

void reverse(struct node *ptr, int a[]) {
    
    // A variable to keep track of array index
    int i = 0;
    
    // Copy data from linked list to array
    while (ptr != NULL) {
        a[i] = ptr -> data;
        i++;
        ptr = ptr -> next;
    }
    
    // Set index to begin reverse procedure
    ptr = head;
    i--;
    
    // Copy data from array to linked list in reverse
    while (ptr != NULL) {
        ptr -> data = a[i];
        i--;
        ptr = ptr -> next;
    }
    
}

Reversing using Sliding Pointers :

  • Take two additional pointers, prev and next, and initialise them with NULL.

  • Copy the next address of each node to the next pointer and then set the next address of node to the prev pointer.

  • Make current as next and prev as current in order to move forward.

  • Do this procedure until current becomes NULL.

  • Set the head node as prev pointer.

void reverse(struct node *ptr) {

    // Consider ptr to the pointer to current node
    struct node *prev = NULL;
    struct node *next = NULL;

    while (ptr != NULL) {
        // Store the address of next node
        next = ptr -> next;
        // Make the next of each node point to its previous
        ptr -> next = prev;
        // Move forward
        prev = ptr;
        ptr = next;
    }
    // Setting the value of head node
    head = prev;   
}

Reversing by Recursion :

void reverse(struct node *prev, struct node *curr) {

    if (curr != NULL) {
        reverse(curr, curr -> next);
        curr -> next = prev;        
    } else {
        head = prev;
    }

}

// Function call : reverse(NULL, head) 

Contributed by Nitin Ranganath

PreviousRemoving Duplicates from a ListNextConcatenating Two Lists

Last updated 5 years ago

Was this helpful?