Iteratively Reverse a linked list using only 2 pointers ( An Interesting Method ) | C ++ program to reverse a linked list using two pointers . ; Link list node ; Function to reverse the linked list using 2 pointers ; Function to push a node ; Function to print linked list ; Driver program to test above function ; Start with the empty list
#include <bits/stdc++.h> NEW_LINE using namespace std ; typedef uintptr_t ut ; struct Node { int data ; struct Node * next ; } ; void reverse ( struct Node * * head_ref ) { struct Node * current = * head_ref ; struct Node * next ; while ( current -> next != NULL ) { next = current -> next ; current -> next = next -> next ; next -> next = ( * head_ref ) ; * head_ref = next ; } } void push ( struct Node * * head_ref , int new_data ) { struct Node * new_node = new Node ; new_node -> data = new_data ; new_node -> next = ( * head_ref ) ; ( * head_ref ) = new_node ; } void printList ( struct Node * head ) { struct Node * temp = head ; while ( temp != NULL ) { printf ( " % d ▁ " , temp -> data ) ; temp = temp -> next ; } } int main ( ) { struct Node * head = NULL ; push ( & head , 20 ) ; push ( & head , 4 ) ; push ( & head , 15 ) ; push ( & head , 85 ) ; printf ( " Given ▁ linked ▁ list STRNEWLINE " ) ; printList ( head ) ; reverse ( & head ) ; printf ( " Reversed Linked list " printList ( head ) ; return 0 ; }