Iteratively Reverse a linked list using only 2 pointers ( An Interesting Method ) | A linked list node ; Function to reverse the linked list using 2 pointers ; Function to push a node ; Function to print linked list ; Start with the empty list
class Node : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . data = 0 NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def reverse ( head_ref ) : NEW_LINE INDENT current = head_ref NEW_LINE next = None NEW_LINE while ( current . next != None ) : NEW_LINE INDENT next = current . next NEW_LINE current . next = next . next NEW_LINE next . next = ( head_ref ) NEW_LINE head_ref = next NEW_LINE DEDENT return head_ref NEW_LINE DEDENT def push ( head_ref , new_data ) : NEW_LINE INDENT new_node = Node ( ) NEW_LINE new_node . data = new_data NEW_LINE new_node . next = ( head_ref ) NEW_LINE ( head_ref ) = new_node NEW_LINE return head_ref NEW_LINE DEDENT def printList ( head ) : NEW_LINE INDENT temp = head NEW_LINE while ( temp != None ) : NEW_LINE INDENT print ( temp . data , end = " ▁ " ) NEW_LINE temp = temp . next NEW_LINE DEDENT DEDENT head = None NEW_LINE head = push ( head , 20 ) NEW_LINE head = push ( head , 4 ) NEW_LINE head = push ( head , 15 ) NEW_LINE head = push ( head , 85 ) NEW_LINE print ( " Given ▁ linked ▁ list " ) NEW_LINE printList ( head ) NEW_LINE head = reverse ( head ) NEW_LINE print ( " Reversed Linked list   " ) NEW_LINE printList ( head ) NEW_LINE