codeparrot/xlcost-text-to-code huggingface dataset:
Rotate a Linked List | Link list node ; This function rotates a linked list counter - clockwise and updates the head . The function assumes that k is smaller than size of linked list . ; Let us understand the below code for example k = 4 and list = 10.20 . 30.40 .50 . 60. ; Traverse till the end . ; Traverse the linked list to k - 1 position which will be last element for rotated array . ; Update the head_ref and last element pointer to None ; Function to push a node ; Allocate node ; Put in the data ; Link the old list off the new node ; Move the head to point to the new node ; Function to print linked list ; Driver code ; Start with the empty list ; Create a list 10.20 . 30.40 .50 . 60
class Node : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . data = 0 NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def rotate ( head_ref , k ) : NEW_LINE INDENT if ( k == 0 ) : NEW_LINE INDENT return NEW_LINE DEDENT current = head_ref NEW_LINE while ( current . next != None ) : NEW_LINE INDENT current = current . next NEW_LINE DEDENT current . next = head_ref NEW_LINE current = head_ref NEW_LINE for i in range ( k - 1 ) : NEW_LINE INDENT current = current . next NEW_LINE DEDENT head_ref = current . next NEW_LINE current . next = None NEW_LINE 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 ( node ) : NEW_LINE INDENT while ( node != None ) : NEW_LINE INDENT print ( node . data , end = ' ▁ ' ) NEW_LINE node = node . next NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = None NEW_LINE for i in range ( 60 , 0 , - 10 ) : NEW_LINE INDENT head = push ( head , i ) NEW_LINE DEDENT print ( " Given ▁ linked ▁ list ▁ " ) NEW_LINE printList ( head ) NEW_LINE head = rotate ( head , 4 ) NEW_LINE print ( " Rotated Linked list   " ) NEW_LINE printList ( head ) NEW_LINE DEDENT
Sort a linked list of 0 s , 1 s and 2 s | Python program to sort a linked list of 0 , 1 and 2 ; head of list ; Linked list Node ; initialise count of 0 1 and 2 as 0 ; count total number of '0' , '1' and '2' * count [ 0 ] will store total number of '0' s * count [ 1 ] will store total number of '1' s * count [ 2 ] will store total number of '2' s ; Let say count [ 0 ] = n1 , count [ 1 ] = n2 and count [ 2 ] = n3 * now start traversing list from head node , * 1 ) fill the list with 0 , till n1 > 0 * 2 ) fill the list with 1 , till n2 > 0 * 3 ) fill the list with 2 , till n3 > 0 ; Inserts a new Node at front of the list . ; 1 & 2 : Allocate the Node & Put in the data ; 3. Make next of new Node as head ; 4. Move the head to point to new Node ; Function to print linked list ; Constructed Linked List is 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 8 -> 9 -> null
class LinkedList ( object ) : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . head = None NEW_LINE DEDENT class Node ( object ) : NEW_LINE INDENT def __init__ ( self , d ) : NEW_LINE INDENT self . data = d NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def sortList ( self ) : NEW_LINE INDENT count = [ 0 , 0 , 0 ] NEW_LINE ptr = self . head NEW_LINE while ptr != None : NEW_LINE INDENT count [ ptr . data ] += 1 NEW_LINE ptr = ptr . next NEW_LINE DEDENT i = 0 NEW_LINE ptr = self . head NEW_LINE while ptr != None : NEW_LINE INDENT if count [ i ] == 0 : NEW_LINE INDENT i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT ptr . data = i NEW_LINE count [ i ] -= 1 NEW_LINE ptr = ptr . next NEW_LINE DEDENT DEDENT DEDENT def push ( self , new_data ) : NEW_LINE INDENT new_node = self . Node ( new_data ) NEW_LINE new_node . next = self . head NEW_LINE self . head = new_node NEW_LINE DEDENT def printList ( self ) : NEW_LINE INDENT temp = self . head NEW_LINE while temp != None : NEW_LINE INDENT print str ( temp . data ) , NEW_LINE temp = temp . next NEW_LINE DEDENT print ' ' NEW_LINE DEDENT DEDENT llist = LinkedList ( ) NEW_LINE llist . push ( 0 ) NEW_LINE llist . push ( 1 ) NEW_LINE llist . push ( 0 ) NEW_LINE llist . push ( 2 ) NEW_LINE llist . push ( 1 ) NEW_LINE llist . push ( 1 ) NEW_LINE llist . push ( 2 ) NEW_LINE llist . push ( 1 ) NEW_LINE llist . push ( 2 ) NEW_LINE print " Linked ▁ List ▁ before ▁ sorting " NEW_LINE llist . printList ( ) NEW_LINE llist . sortList ( ) NEW_LINE print " Linked ▁ List ▁ after ▁ sorting " NEW_LINE llist . printList ( ) NEW_LINE
Flatten a multilevel linked list | A linked list node has data , next pointer and child pointer
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE self . child = None NEW_LINE DEDENT DEDENT
Rearrange a linked list such that all even and odd positioned nodes are together | Linked List Node ; A utility function to create a new node ; Rearranges given linked list such that all even positioned nodes are before odd positioned . Returns new head of linked List . ; Corner case ; Initialize first nodes of even and odd lists ; Remember the first node of even list so that we can connect the even list at the end of odd list . ; If there are no more nodes , then connect first node of even list to the last node of odd list ; Connecting odd nodes ; If there are NO more even nodes after current odd . ; Connecting even nodes ; A utility function to print a linked list ; Function to insert a new node at the beginning ; Driver code
class Node : NEW_LINE INDENT def __init__ ( self , d ) : NEW_LINE INDENT self . data = d NEW_LINE self . next = None NEW_LINE DEDENT DEDENT class LinkedList : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . head = None NEW_LINE DEDENT def newNode ( self , key ) : NEW_LINE INDENT temp = Node ( key ) NEW_LINE self . next = None NEW_LINE return temp NEW_LINE DEDENT def rearrangeEvenOdd ( self , head ) : NEW_LINE INDENT if ( self . head == None ) : NEW_LINE INDENT return None NEW_LINE DEDENT odd = self . head NEW_LINE even = self . head . next NEW_LINE evenFirst = even NEW_LINE while ( 1 == 1 ) : NEW_LINE INDENT if ( odd == None or even == None or ( even . next ) == None ) : NEW_LINE INDENT odd . next = evenFirst NEW_LINE break NEW_LINE DEDENT odd . next = even . next NEW_LINE odd = even . next NEW_LINE if ( odd . next == None ) : NEW_LINE INDENT even . next = None NEW_LINE odd . next = evenFirst NEW_LINE break NEW_LINE DEDENT even . next = odd . next NEW_LINE even = odd . next NEW_LINE DEDENT return head NEW_LINE DEDENT def printlist ( self , node ) : NEW_LINE INDENT while ( node != None ) : NEW_LINE INDENT print ( node . data , end = " " ) NEW_LINE print ( " - > " , end = " " ) NEW_LINE node = node . next NEW_LINE DEDENT print ( " NULL " ) NEW_LINE DEDENT def push ( self , new_data ) : NEW_LINE INDENT new_node = Node ( new_data ) NEW_LINE new_node . next = self . head NEW_LINE self . head = new_node NEW_LINE DEDENT DEDENT ll = LinkedList ( ) NEW_LINE ll . push ( 5 ) NEW_LINE ll . push ( 4 ) NEW_LINE ll . push ( 3 ) NEW_LINE ll . push ( 2 ) NEW_LINE ll . push ( 1 ) NEW_LINE print ( " Given ▁ Linked ▁ List " ) NEW_LINE ll . printlist ( ll . head ) NEW_LINE start = ll . rearrangeEvenOdd ( ll . head ) NEW_LINE print ( " Modified Linked List " ) NEW_LINE ll . printlist ( start ) NEW_LINE
Add 1 to a number represented as linked list | Node class ; Function to create a new node with given data ; Recursively add 1 from end to beginning and returns carry after all nodes are processed . ; If linked list is empty , then return carry ; Add carry returned be next node call ; Update data and return new carry ; This function mainly uses addWithCarry ( ) . ; Add 1 to linked list from end to beginning ; If there is carry after processing all nodes , then we need to add a new node to linked list ; New node becomes head now ; A utility function to print a linked list ; Driver program to test above function
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 newNode ( data ) : NEW_LINE INDENT new_node = Node ( 0 ) NEW_LINE new_node . data = data NEW_LINE new_node . next = None NEW_LINE return new_node NEW_LINE DEDENT def addWithCarry ( head ) : NEW_LINE INDENT if ( head == None ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT res = head . data + addWithCarry ( head . next ) NEW_LINE head . data = int ( ( res ) % 10 ) NEW_LINE return int ( ( res ) / 10 ) NEW_LINE DEDENT def addOne ( head ) : NEW_LINE INDENT carry = addWithCarry ( head ) NEW_LINE if ( carry != 0 ) : NEW_LINE INDENT newNode = Node ( 0 ) NEW_LINE newNode . data = carry NEW_LINE newNode . next = head NEW_LINE return newNode NEW_LINE DEDENT return head NEW_LINE DEDENT def printList ( node ) : NEW_LINE INDENT while ( node != None ) : NEW_LINE INDENT print ( node . data , end = " " ) NEW_LINE node = node . next NEW_LINE DEDENT print ( " " ) NEW_LINE DEDENT head = newNode ( 1 ) NEW_LINE head . next = newNode ( 9 ) NEW_LINE head . next . next = newNode ( 9 ) NEW_LINE head . next . next . next = newNode ( 9 ) NEW_LINE print ( " List ▁ is ▁ " ) NEW_LINE printList ( head ) NEW_LINE head = addOne ( head ) NEW_LINE print ( " Resultant list is   " ) NEW_LINE printList ( head ) NEW_LINE
Point arbit pointer to greatest value right side node in a linked list | Node class ; Function to reverse the linked list ; This function populates arbit pointer in every node to the greatest value to its right . ; Reverse given linked list ; Initialize pointer to maximum value node ; Traverse the reversed list ; Connect max through arbit pointer ; Update max if required ; Move ahead in reversed list ; Reverse modified linked list and return head . ; Utility function to print result linked list ; Function to create a new node with given data ; Driver code
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE self . arbit = None NEW_LINE DEDENT DEDENT def reverse ( head ) : NEW_LINE INDENT prev = None NEW_LINE current = head NEW_LINE next = 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 return prev NEW_LINE DEDENT def populateArbit ( head ) : NEW_LINE INDENT head = reverse ( head ) NEW_LINE max = head NEW_LINE temp = head . next NEW_LINE while ( temp != None ) : NEW_LINE INDENT temp . arbit = max NEW_LINE if ( max . data < temp . data ) : NEW_LINE INDENT max = temp NEW_LINE DEDENT temp = temp . next NEW_LINE DEDENT return reverse ( head ) NEW_LINE DEDENT def printNextArbitPointers ( node ) : NEW_LINE INDENT print ( " Node Next Pointer Arbit Pointer " ) NEW_LINE while ( node != None ) : NEW_LINE INDENT print ( node . data , " TABSYMBOL TABSYMBOL " , end = " " ) NEW_LINE if ( node . next != None ) : NEW_LINE INDENT print ( node . next . data , " TABSYMBOL TABSYMBOL " , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " None " , " TABSYMBOL TABSYMBOL " , end = " " ) NEW_LINE DEDENT if ( node . arbit != None ) : NEW_LINE INDENT print ( node . arbit . data , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " None " , end = " " ) NEW_LINE DEDENT print ( " " ) NEW_LINE node = node . next NEW_LINE DEDENT DEDENT def newNode ( data ) : NEW_LINE INDENT new_node = Node ( 0 ) NEW_LINE new_node . data = data NEW_LINE new_node . next = None NEW_LINE return new_node NEW_LINE DEDENT head = newNode ( 5 ) NEW_LINE head . next = newNode ( 10 ) NEW_LINE head . next . next = newNode ( 2 ) NEW_LINE head . next . next . next = newNode ( 3 ) NEW_LINE head = populateArbit ( head ) NEW_LINE print ( " Resultant Linked List is : " ) NEW_LINE printNextArbitPointers ( head ) NEW_LINE
Point arbit pointer to greatest value right side node in a linked list | Node class ; This function populates arbit pointer in every node to the greatest value to its right . ; using static maxNode to keep track of maximum orbit node address on right side ; if head is null simply return the list ; if head . next is null it means we reached at the last node just update the max and maxNode ; Calling the populateArbit to the next node ; updating the arbit node of the current node with the maximum value on the right side ; if current Node value id greater then the previous right node then update it ; Utility function to prresult linked list ; Driver code
class newNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE self . arbit = None NEW_LINE DEDENT DEDENT maxNode = newNode ( None ) NEW_LINE def populateArbit ( head ) : NEW_LINE INDENT global maxNode NEW_LINE if ( head == None ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( head . next == None ) : NEW_LINE INDENT maxNode = head NEW_LINE return NEW_LINE DEDENT populateArbit ( head . next ) NEW_LINE head . arbit = maxNode NEW_LINE if ( head . data > maxNode . data and maxNode . data != None ) : NEW_LINE INDENT maxNode = head NEW_LINE DEDENT return NEW_LINE DEDENT def printNextArbitPointers ( node ) : NEW_LINE INDENT print ( " Node TABSYMBOL Next ▁ Pointer TABSYMBOL Arbit ▁ Pointer " ) NEW_LINE while ( node != None ) : NEW_LINE INDENT print ( node . data , " TABSYMBOL TABSYMBOL " , end = " " ) NEW_LINE if ( node . next ) : NEW_LINE INDENT print ( node . next . data , " TABSYMBOL TABSYMBOL " , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NULL " , " TABSYMBOL TABSYMBOL " , end = " " ) NEW_LINE DEDENT if ( node . arbit ) : NEW_LINE INDENT print ( node . arbit . data , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NULL " , end = " " ) NEW_LINE DEDENT print ( ) NEW_LINE node = node . next NEW_LINE DEDENT DEDENT head = newNode ( 5 ) NEW_LINE head . next = newNode ( 10 ) NEW_LINE head . next . next = newNode ( 2 ) NEW_LINE head . next . next . next = newNode ( 3 ) NEW_LINE populateArbit ( head ) NEW_LINE print ( " Resultant ▁ Linked ▁ List ▁ is : " ) NEW_LINE printNextArbitPointers ( head ) NEW_LINE
Check if a linked list of strings forms a palindrome | Node class ;  ; A utility function to check if str is palindrome or not ; Match characters from beginning and end . ; Returns true if string formed by linked list is palindrome ; Append all nodes to form a string ; Utility function to print the linked LinkedList ; Driver program to test above function
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT class LinkedList : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . head = None NEW_LINE DEDENT def isPalindromeUtil ( self , string ) : NEW_LINE INDENT return ( string == string [ : : - 1 ] ) NEW_LINE DEDENT def isPalindrome ( self ) : NEW_LINE INDENT node = self . head NEW_LINE temp = [ ] NEW_LINE while ( node is not None ) : NEW_LINE INDENT temp . append ( node . data ) NEW_LINE node = node . next NEW_LINE DEDENT string = " " . join ( temp ) NEW_LINE return self . isPalindromeUtil ( string ) NEW_LINE DEDENT def printList ( self ) : NEW_LINE INDENT temp = self . head NEW_LINE while ( temp ) : NEW_LINE INDENT print temp . data , NEW_LINE temp = temp . next NEW_LINE DEDENT DEDENT DEDENT llist = LinkedList ( ) NEW_LINE llist . head = Node ( ' a ' ) NEW_LINE llist . head . next = Node ( ' bc ' ) NEW_LINE llist . head . next . next = Node ( " d " ) NEW_LINE llist . head . next . next . next = Node ( " dcb " ) NEW_LINE llist . head . next . next . next . next = Node ( " a " ) NEW_LINE print " true " if llist . isPalindrome ( ) else " false " NEW_LINE
Delete last occurrence of an item from linked list | A linked list Node ; Function to delete the last occurrence ; If found key , update ; If the last occurrence is the last node ; If it is not the last node ; Utility function to create a new node with given key ; This function prints contents of linked list starting from the given Node ; Driver code
class Node : NEW_LINE INDENT def __init__ ( self , new_data ) : NEW_LINE INDENT self . data = new_data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def deleteLast ( head , x ) : NEW_LINE INDENT temp = head NEW_LINE ptr = None NEW_LINE while ( temp != None ) : NEW_LINE INDENT if ( temp . data == x ) : NEW_LINE INDENT ptr = temp NEW_LINE DEDENT temp = temp . next NEW_LINE DEDENT if ( ptr != None and ptr . next == None ) : NEW_LINE INDENT temp = head NEW_LINE while ( temp . next != ptr ) : NEW_LINE INDENT temp = temp . next NEW_LINE DEDENT temp . next = None NEW_LINE DEDENT if ( ptr != None and ptr . next != None ) : NEW_LINE INDENT ptr . data = ptr . next . data NEW_LINE temp = ptr . next NEW_LINE ptr . next = ptr . next . next NEW_LINE DEDENT return head NEW_LINE DEDENT def newNode ( x ) : NEW_LINE INDENT node = Node ( 0 ) NEW_LINE node . data = x NEW_LINE node . next = None NEW_LINE return node NEW_LINE DEDENT def display ( head ) : NEW_LINE INDENT temp = head NEW_LINE if ( head == None ) : NEW_LINE INDENT print ( " None " ) NEW_LINE return NEW_LINE DEDENT while ( temp != None ) : NEW_LINE INDENT print ( temp . data , " ▁ - > ▁ " , end = " " ) NEW_LINE temp = temp . next NEW_LINE DEDENT print ( " None " ) NEW_LINE DEDENT head = newNode ( 1 ) NEW_LINE head . next = newNode ( 2 ) NEW_LINE head . next . next = newNode ( 3 ) NEW_LINE head . next . next . next = newNode ( 4 ) NEW_LINE head . next . next . next . next = newNode ( 5 ) NEW_LINE head . next . next . next . next . next = newNode ( 4 ) NEW_LINE head . next . next . next . next . next . next = newNode ( 4 ) NEW_LINE print ( " Created ▁ Linked ▁ list : ▁ " ) NEW_LINE display ( head ) NEW_LINE head = deleteLast ( head , 4 ) NEW_LINE print ( " List ▁ after ▁ deletion ▁ of ▁ 4 : ▁ " ) NEW_LINE display ( head ) NEW_LINE
Flatten a multi |
def flattenList2 ( head ) : NEW_LINE INDENT headcop = head NEW_LINE save = [ ] NEW_LINE save . append ( head ) NEW_LINE prev = None NEW_LINE while ( len ( save ) != 0 ) : NEW_LINE INDENT temp = save [ - 1 ] NEW_LINE save . pop ( ) NEW_LINE if ( temp . next ) : NEW_LINE INDENT save . append ( temp . next ) NEW_LINE DEDENT if ( temp . down ) : NEW_LINE INDENT save . append ( temp . down ) NEW_LINE DEDENT if ( prev != None ) : NEW_LINE INDENT prev . next = temp NEW_LINE DEDENT prev = temp NEW_LINE DEDENT return headcop NEW_LINE DEDENT
Length of longest palindrome list in a linked list using O ( 1 ) extra space | Linked List node ; function for counting the common elements ; loop to count coomon in the list starting from node a and b ; increment the count for same values ; Returns length of the longest palindrome sublist in given list ; loop till the end of the linked list ; The sublist from head to current reversed . ; check for odd length palindrome by finding longest common list elements beginning from prev and from next ( We exclude curr ) ; check for even length palindrome by finding longest common list elements beginning from curr and from next ; update prev and curr for next iteration ; Utility function to create a new list node ; Let us create a linked lists to test the functions Created list is a : 2 -> 4 -> 3 -> 4 -> 2 -> 15
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 countCommon ( a , b ) : NEW_LINE INDENT count = 0 NEW_LINE while ( a != None and b != None ) : NEW_LINE INDENT if ( a . data == b . data ) : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT a = a . next NEW_LINE b = b . next NEW_LINE DEDENT return count NEW_LINE DEDENT def maxPalindrome ( head ) : NEW_LINE INDENT result = 0 NEW_LINE prev = None NEW_LINE curr = head NEW_LINE while ( curr != None ) : NEW_LINE INDENT next = curr . next NEW_LINE curr . next = prev NEW_LINE result = max ( result , 2 * countCommon ( prev , next ) + 1 ) NEW_LINE result = max ( result , 2 * countCommon ( curr , next ) ) NEW_LINE prev = curr NEW_LINE curr = next NEW_LINE DEDENT return result NEW_LINE DEDENT def newNode ( key ) : NEW_LINE INDENT temp = Node ( 0 ) NEW_LINE temp . data = key NEW_LINE temp . next = None NEW_LINE return temp NEW_LINE DEDENT head = newNode ( 2 ) NEW_LINE head . next = newNode ( 4 ) NEW_LINE head . next . next = newNode ( 3 ) NEW_LINE head . next . next . next = newNode ( 4 ) NEW_LINE head . next . next . next . next = newNode ( 2 ) NEW_LINE head . next . next . next . next . next = newNode ( 15 ) NEW_LINE print ( maxPalindrome ( head ) ) NEW_LINE
Implementing Iterator pattern of a single Linked List |  ; Creating a list ; Elements to be added at the end . in the above created list . ; Elements of list are retrieved through iterator .
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT list = [ ] NEW_LINE list . append ( 1 ) NEW_LINE list . append ( 2 ) NEW_LINE list . append ( 3 ) NEW_LINE for it in list : NEW_LINE INDENT print ( it , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT
Move all occurrences of an element to end in a linked list | Linked List node ; A urility function to create a new node . ; Utility function to print the elements in Linked list ; Moves all occurrences of given key to end of linked list . ; Keeps track of locations where key is present . ; Traverse list ; If current pointer is not same as pointer to a key location , then we must have found a key in linked list . We swap data of pCrawl and pKey and move pKey to next position . ; Find next position where key is present ; Moving to next Node ; Driver code
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 newNode ( x ) : NEW_LINE INDENT temp = Node ( 0 ) NEW_LINE temp . data = x NEW_LINE temp . next = None NEW_LINE return temp 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 print ( ) NEW_LINE DEDENT def moveToEnd ( head , key ) : NEW_LINE INDENT pKey = head NEW_LINE pCrawl = head NEW_LINE while ( pCrawl != None ) : NEW_LINE INDENT if ( pCrawl != pKey and pCrawl . data != key ) : NEW_LINE INDENT pKey . data = pCrawl . data NEW_LINE pCrawl . data = key NEW_LINE pKey = pKey . next NEW_LINE DEDENT if ( pKey . data != key ) : NEW_LINE INDENT pKey = pKey . next NEW_LINE DEDENT pCrawl = pCrawl . next NEW_LINE DEDENT return head NEW_LINE DEDENT head = newNode ( 10 ) NEW_LINE head . next = newNode ( 20 ) NEW_LINE head . next . next = newNode ( 10 ) NEW_LINE head . next . next . next = newNode ( 30 ) NEW_LINE head . next . next . next . next = newNode ( 40 ) NEW_LINE head . next . next . next . next . next = newNode ( 10 ) NEW_LINE head . next . next . next . next . next . next = newNode ( 60 ) NEW_LINE print ( " Before moveToEnd ( ) , the Linked list is " ) NEW_LINE printList ( head ) NEW_LINE key = 10 NEW_LINE head = moveToEnd ( head , key ) NEW_LINE print ( " After moveToEnd ( ) , the Linked list is " ) NEW_LINE printList ( head ) NEW_LINE