Modify contents of Linked List | Linked list node ; Function to insert a node at the beginning of the linked list ; allocate node ; put in the data ; link the old list at the end of the new node ; move the head to point to the new node ; Split the nodes of the given list into front and back halves , and return the two lists using the reference parameters . Uses the fast / slow pointer strategy . ; Advance ' fast ' two nodes , and advance ' slow ' one node ; ' slow ' is before the midpoint in the list , so split it in two at that point . ; Function to reverse the linked list ; perfrom the required subtraction operation on the 1 st half of the linked list ; traversing both the lists simultaneously ; subtraction operation and node data modification ; function to concatenate the 2 nd ( back ) list at the end of the 1 st ( front ) list and returns the head of the new list ; function to modify the contents of the linked list ; if list is empty or contains only single node ; split the list into two halves front and back lists ; reverse the 2 nd ( back ) list ; modify the contents of 1 st half ; agains reverse the 2 nd ( back ) list ; concatenating the 2 nd list back to the end of the 1 st list ; pointer to the modified list ; function to print the linked list ; Driver Code ; creating the linked list ; modify the linked list ; print the modified linked list
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def push ( head_ref , new_data ) : NEW_LINE INDENT new_node = Node ( 0 ) 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 front = None NEW_LINE back = None NEW_LINE def frontAndBackSplit ( head ) : NEW_LINE INDENT global front NEW_LINE global back NEW_LINE slow = None NEW_LINE fast = None NEW_LINE slow = head NEW_LINE fast = head . next NEW_LINE while ( fast != None ) : NEW_LINE INDENT fast = fast . next NEW_LINE if ( fast != None ) : NEW_LINE INDENT slow = slow . next NEW_LINE fast = fast . next NEW_LINE DEDENT DEDENT front = head NEW_LINE back = slow . next NEW_LINE slow . next = None NEW_LINE return head NEW_LINE DEDENT def reverseList ( head_ref ) : NEW_LINE INDENT current = None NEW_LINE prev = None NEW_LINE next = None NEW_LINE current = head_ref NEW_LINE prev = None NEW_LINE while ( current != None ) : NEW_LINE INDENT next = current . next NEW_LINE current . next = prev NEW_LINE prev = current NEW_LINE current = next NEW_LINE DEDENT head_ref = prev NEW_LINE return head_ref NEW_LINE DEDENT def modifyTheContentsOf1stHalf ( ) : NEW_LINE INDENT global front NEW_LINE global back NEW_LINE front1 = front NEW_LINE back1 = back NEW_LINE while ( back1 != None ) : NEW_LINE INDENT front1 . data = front1 . data - back1 . data NEW_LINE front1 = front1 . next NEW_LINE back1 = back1 . next NEW_LINE DEDENT DEDENT def concatFrontAndBackList ( front , back ) : NEW_LINE INDENT head = front NEW_LINE if ( front == None ) : NEW_LINE INDENT return back NEW_LINE DEDENT while ( front . next != None ) : NEW_LINE INDENT front = front . next NEW_LINE DEDENT front . next = back NEW_LINE return head NEW_LINE DEDENT def modifyTheList ( head ) : NEW_LINE INDENT global front NEW_LINE global back NEW_LINE if ( head == None or head . next == None ) : NEW_LINE INDENT return head NEW_LINE DEDENT front = None NEW_LINE back = None NEW_LINE frontAndBackSplit ( head ) NEW_LINE back = reverseList ( back ) NEW_LINE modifyTheContentsOf1stHalf ( ) NEW_LINE back = reverseList ( back ) NEW_LINE head = concatFrontAndBackList ( front , back ) NEW_LINE return head NEW_LINE DEDENT def printList ( head ) : NEW_LINE INDENT if ( head == None ) : NEW_LINE INDENT return NEW_LINE DEDENT while ( head . next != None ) : NEW_LINE INDENT print ( head . data , " ▁ - > ▁ " , end = " " ) NEW_LINE head = head . next NEW_LINE DEDENT print ( head . data ) NEW_LINE DEDENT head = None NEW_LINE head = push ( head , 10 ) NEW_LINE head = push ( head , 7 ) NEW_LINE head = push ( head , 12 ) NEW_LINE head = push ( head , 8 ) NEW_LINE head = push ( head , 9 ) NEW_LINE head = push ( head , 2 ) NEW_LINE head = modifyTheList ( head ) NEW_LINE print ( " Modified ▁ List : " ) NEW_LINE printList ( head ) NEW_LINE