codeparrot/xlcost-text-to-code huggingface dataset:
Move all occurrences of an element to end in a linked list | A Linked list Node ; Function to remove key to end ; Node to keep pointing to tail ; Node to point to last of linked list ; Node prev2 to point to previous when head . data != key ; Loop to perform operations to remove key to end ; Function to display linked list ; 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 ( x ) NEW_LINE return temp NEW_LINE DEDENT def keyToEnd ( head , key ) : NEW_LINE INDENT tail = head NEW_LINE if ( head == None ) : NEW_LINE INDENT return None NEW_LINE DEDENT while ( tail . next != None ) : NEW_LINE INDENT tail = tail . next NEW_LINE DEDENT last = tail NEW_LINE current = head NEW_LINE prev = None NEW_LINE prev2 = None NEW_LINE while ( current != tail ) : NEW_LINE INDENT if ( current . data == key and prev2 == None ) : NEW_LINE INDENT prev = current NEW_LINE current = current . next NEW_LINE head = current NEW_LINE last . next = prev NEW_LINE last = last . next NEW_LINE last . next = None NEW_LINE prev = None NEW_LINE DEDENT else : NEW_LINE INDENT if ( current . data == key and prev2 != None ) : NEW_LINE INDENT prev = current NEW_LINE current = current . next NEW_LINE prev2 . next = current NEW_LINE last . next = prev NEW_LINE last = last . next NEW_LINE last . next = None NEW_LINE DEDENT elif ( current != tail ) : NEW_LINE INDENT prev2 = current NEW_LINE current = current . next NEW_LINE DEDENT DEDENT DEDENT return head 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 if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 5 ) NEW_LINE root . next = newNode ( 2 ) NEW_LINE root . next . next = newNode ( 2 ) NEW_LINE root . next . next . next = newNode ( 7 ) NEW_LINE root . next . next . next . next = newNode ( 2 ) NEW_LINE root . next . next . next . next . next = newNode ( 2 ) NEW_LINE root . next . next . next . next . next . next = newNode ( 2 ) NEW_LINE key = 2 NEW_LINE print ( " Linked ▁ List ▁ before ▁ operations ▁ : " ) NEW_LINE printList ( root ) NEW_LINE print ( " Linked ▁ List ▁ after ▁ operations ▁ : " ) NEW_LINE root = keyToEnd ( root , key ) NEW_LINE printList ( root ) NEW_LINE DEDENT
Remove every k | Python3 program to delete every k - th Node of a singly linked list . ; Linked list Node ; To remove complete list ( Needed forcase when k is 1 ) ; Deletes every k - th node and returns head of modified list . ; If linked list is empty ; Initialize ptr and prev before starting traversal . ; Traverse list and delete every k - th node ; increment Node count ; check if count is equal to k if yes , then delete current Node ; put the next of current Node in the next of previous Node delete ( prev . next ) ; set count = 0 to reach further k - th Node ; update prev if count is not 0 ; Function to print linked list ; Utility function to create a new node . ; Driver Code ; Start with the empty list
import math NEW_LINE 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 freeList ( node ) : NEW_LINE INDENT while ( node != None ) : NEW_LINE INDENT next = node . next NEW_LINE node = next NEW_LINE DEDENT return node NEW_LINE DEDENT def deleteKthNode ( head , k ) : NEW_LINE INDENT if ( head == None ) : NEW_LINE INDENT return None NEW_LINE DEDENT if ( k == 1 ) : NEW_LINE INDENT freeList ( head ) NEW_LINE return None NEW_LINE DEDENT ptr = head NEW_LINE prev = None NEW_LINE count = 0 NEW_LINE while ( ptr != None ) : NEW_LINE INDENT count = count + 1 NEW_LINE if ( k == count ) : NEW_LINE INDENT prev . next = ptr . next NEW_LINE count = 0 NEW_LINE DEDENT if ( count != 0 ) : NEW_LINE INDENT prev = ptr NEW_LINE DEDENT ptr = prev . next NEW_LINE DEDENT return head NEW_LINE DEDENT def displayList ( 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 def newNode ( x ) : NEW_LINE INDENT temp = Node ( x ) NEW_LINE temp . data = x NEW_LINE temp . next = None NEW_LINE return temp NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT 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 ( 6 ) NEW_LINE head . next . next . next . next . next . next = newNode ( 7 ) NEW_LINE head . next . next . next . next . next . next . next = newNode ( 8 ) NEW_LINE k = 3 NEW_LINE head = deleteKthNode ( head , k ) NEW_LINE displayList ( head ) NEW_LINE DEDENT
Check whether the length of given linked list is Even or Odd | Defining structure ; Function to check the length of linklist ; Push function ; Allocating node ; Next of new node to head ; head points to new node ; Driver code ; Adding elements to Linked List ; Checking for length of linklist
class Node : NEW_LINE INDENT def __init__ ( self , d ) : NEW_LINE INDENT self . data = d NEW_LINE self . next = None NEW_LINE self . head = None NEW_LINE DEDENT def LinkedListLength ( self ) : NEW_LINE INDENT while ( self . head != None and self . head . next != None ) : NEW_LINE INDENT self . head = self . head . next . next NEW_LINE DEDENT if ( self . head == None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT return 1 NEW_LINE DEDENT def push ( self , info ) : NEW_LINE INDENT node = Node ( info ) NEW_LINE node . next = ( self . head ) NEW_LINE ( self . head ) = node NEW_LINE DEDENT DEDENT head = Node ( 0 ) NEW_LINE head . push ( 4 ) NEW_LINE head . push ( 5 ) NEW_LINE head . push ( 7 ) NEW_LINE head . push ( 2 ) NEW_LINE head . push ( 9 ) NEW_LINE head . push ( 6 ) NEW_LINE head . push ( 1 ) NEW_LINE head . push ( 2 ) NEW_LINE head . push ( 0 ) NEW_LINE head . push ( 5 ) NEW_LINE head . push ( 5 ) NEW_LINE check = head . LinkedListLength ( ) NEW_LINE if ( check == 0 ) : NEW_LINE INDENT print ( " Even " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Odd " ) NEW_LINE DEDENT
Find the sum of last n nodes of the given 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 to the new node ; move the head to point to the new node ; utility function to find the sum of last ' n ' nodes ; if n == 0 ; traverses the list from left to right ; push the node ' s ▁ data ▁ onto ▁ the ▁ stack ▁ ' st ; move to next node ; pop ' n ' nodes from ' st ' and add them ; required sum ; Driver Code ; create linked list 10.6 .8 .4 .12
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT head = None NEW_LINE n = 0 NEW_LINE sum = 0 NEW_LINE def push ( head_ref , new_data ) : NEW_LINE INDENT global head NEW_LINE 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 head = head_ref NEW_LINE DEDENT def sumOfLastN_NodesUtil ( head , n ) : NEW_LINE INDENT global sum NEW_LINE if ( n <= 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT st = [ ] NEW_LINE sum = 0 NEW_LINE while ( head != None ) : NEW_LINE INDENT st . append ( head . data ) NEW_LINE head = head . next NEW_LINE DEDENT while ( n ) : NEW_LINE INDENT n -= 1 NEW_LINE sum += st [ 0 ] NEW_LINE st . pop ( 0 ) NEW_LINE DEDENT return sum NEW_LINE DEDENT head = None NEW_LINE push ( head , 12 ) NEW_LINE push ( head , 4 ) NEW_LINE push ( head , 8 ) NEW_LINE push ( head , 6 ) NEW_LINE push ( head , 10 ) NEW_LINE n = 2 NEW_LINE print ( " Sum ▁ of ▁ last " , n , " nodes ▁ = " , sumOfLastN_NodesUtil ( head , n ) ) NEW_LINE
Find the sum of last n nodes of the given Linked List | A 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 to the new Node ; Move the head to poto the new Node ; utility function to find the sum of last ' n ' Nodes ; if n == 0 ; reverse the linked list ; traverse the 1 st ' n ' Nodes of the reversed linked list and add them ; accumulate Node ' s ▁ data ▁ to ▁ ' sum ; move to next Node ; reverse back the linked list ; required sum ; Driver code ; create linked list 10.6 .8 .4 .12
class Node : NEW_LINE INDENT def __init__ ( self , x ) : NEW_LINE INDENT self . data = x NEW_LINE self . next = None NEW_LINE DEDENT DEDENT head = None NEW_LINE def push ( head_ref , new_data ) : NEW_LINE INDENT new_Node = Node ( new_data ) NEW_LINE new_Node . data = new_data NEW_LINE new_Node . next = head_ref NEW_LINE head_ref = new_Node NEW_LINE head = head_ref NEW_LINE return head NEW_LINE DEDENT def reverseList ( ) : NEW_LINE INDENT global head ; NEW_LINE current , prev , next = None , None , None ; NEW_LINE current = head ; 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 = prev ; NEW_LINE DEDENT def sumOfLastN_NodesUtil ( n ) : NEW_LINE INDENT if ( n <= 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT reverseList ( ) ; NEW_LINE sum = 0 ; NEW_LINE current = head ; NEW_LINE while ( current != None and n > 0 ) : NEW_LINE INDENT sum += current . data ; NEW_LINE current = current . next ; NEW_LINE n -= 1 ; NEW_LINE DEDENT reverseList ( ) ; NEW_LINE return sum ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = push ( head , 12 ) NEW_LINE head = push ( head , 4 ) NEW_LINE head = push ( head , 8 ) NEW_LINE head = push ( head , 6 ) NEW_LINE head = push ( head , 10 ) NEW_LINE n = 2 ; NEW_LINE print ( " Sum ▁ of ▁ last ▁ " , n , " ▁ Nodes ▁ = ▁ " , sumOfLastN_NodesUtil ( n ) ) ; NEW_LINE DEDENT
Find the sum of last n nodes of the given Linked List | A 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 to the new Node ; Move the head to poto the new Node ; Utility function to find the sum of last ' n ' Nodes ; If n == 0 ; Calculate the length of the linked list ; Count of first ( len - n ) Nodes ; Just traverse the 1 st ' c ' Nodes ; Move to next Node ; Now traverse the last ' n ' Nodes and add them ; Accumulate Node 's data to sum ; Move to next Node ; Required sum ; Driver code ; Create linked list 10 -> 6 -> 8 -> 4 -> 12
class Node : NEW_LINE INDENT def __init__ ( self , x ) : NEW_LINE INDENT self . data = x NEW_LINE self . next = None NEW_LINE DEDENT DEDENT head = None NEW_LINE def push ( head_ref , new_data ) : NEW_LINE INDENT new_Node = Node ( new_data ) NEW_LINE new_Node . data = new_data NEW_LINE new_Node . next = head_ref NEW_LINE head_ref = new_Node NEW_LINE head = head_ref NEW_LINE return head NEW_LINE DEDENT def sumOfLastN_NodesUtil ( head , n ) : NEW_LINE INDENT if ( n <= 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT sum = 0 NEW_LINE len = 0 NEW_LINE temp = head NEW_LINE while ( temp != None ) : NEW_LINE INDENT len += 1 NEW_LINE temp = temp . next NEW_LINE DEDENT c = len - n NEW_LINE temp = head NEW_LINE while ( temp != None and c > 0 ) : NEW_LINE INDENT temp = temp . next NEW_LINE c -= 1 NEW_LINE DEDENT while ( temp != None ) : NEW_LINE INDENT sum += temp . data NEW_LINE temp = temp . next NEW_LINE DEDENT return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = push ( head , 12 ) NEW_LINE head = push ( head , 4 ) NEW_LINE head = push ( head , 8 ) NEW_LINE head = push ( head , 6 ) NEW_LINE head = push ( head , 10 ) NEW_LINE n = 2 NEW_LINE print ( " Sum ▁ of ▁ last ▁ " , n , " ▁ Nodes ▁ = ▁ " , sumOfLastN_NodesUtil ( head , n ) ) NEW_LINE DEDENT
In | Structure for a linked list node ; Given a reference ( pointer to pointer ) to the head of a list and an int , push a new node on the front of the list . ; 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 merge two sorted linked lists LL1 and LL2 without using any extra space . ; Run till either one of a or b runs out ; For each element of LL1 , compare it with first element of LL2 . ; Swap the two elements involved if LL1 has a greater element ; To keep LL2 sorted , place first element of LL2 at its correct place ; Find mismatch by traversing the second linked list once ; Correct the pointers ; Move LL1 pointer to next element ; Function to print the linked link ; Driver code
class Node : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . data = 0 NEW_LINE self . next = None NEW_LINE DEDENT 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 mergeLists ( a , b ) : NEW_LINE INDENT while ( a and b ) : NEW_LINE INDENT if ( a . data > b . data ) : NEW_LINE INDENT a . data , b . data = b . data , a . data NEW_LINE temp = b NEW_LINE if ( b . next and b . data > b . next . data ) : NEW_LINE INDENT b = b . next NEW_LINE ptr = b NEW_LINE prev = None NEW_LINE while ( ptr and ptr . data < temp . data ) : NEW_LINE INDENT prev = ptr NEW_LINE ptr = ptr . next NEW_LINE DEDENT prev . next = temp NEW_LINE temp . next = ptr NEW_LINE DEDENT DEDENT a = a . next NEW_LINE DEDENT DEDENT def printList ( head ) : NEW_LINE INDENT while ( head ) : NEW_LINE INDENT print ( head . data , end = ' - > ' ) NEW_LINE head = head . next NEW_LINE DEDENT print ( ' NULL ' ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = None NEW_LINE a = push ( a , 10 ) NEW_LINE a = push ( a , 8 ) NEW_LINE a = push ( a , 7 ) NEW_LINE a = push ( a , 4 ) NEW_LINE a = push ( a , 2 ) NEW_LINE b = None NEW_LINE b = push ( b , 12 ) NEW_LINE b = push ( b , 3 ) NEW_LINE b = push ( b , 1 ) NEW_LINE mergeLists ( a , b ) NEW_LINE print ( " First ▁ List : ▁ " , end = ' ' ) NEW_LINE printList ( a ) NEW_LINE print ( " Second ▁ List : ▁ " , end = ' ' ) NEW_LINE printList ( b ) NEW_LINE DEDENT
Delete middle of linked list | Link list Node ; Count of nodes ; Deletes middle node and returns head of the modified list ; Base cases ; Find the count of nodes ; Find the middle node ; Delete the middle node ; Delete the middle node ; A utility function to print a given linked list ; Utility function to create a new node . ; Driver Code ; 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 countOfNodes ( head ) : NEW_LINE INDENT count = 0 NEW_LINE while ( head != None ) : NEW_LINE INDENT head = head . next NEW_LINE count += 1 NEW_LINE DEDENT return count NEW_LINE DEDENT def deleteMid ( head ) : NEW_LINE INDENT if ( head == None ) : NEW_LINE INDENT return None NEW_LINE DEDENT if ( head . next == None ) : NEW_LINE INDENT del head NEW_LINE return None NEW_LINE DEDENT copyHead = head NEW_LINE count = countOfNodes ( head ) NEW_LINE mid = count // 2 NEW_LINE while ( mid > 1 ) : NEW_LINE INDENT mid -= 1 NEW_LINE head = head . next NEW_LINE DEDENT head . next = head . next . next NEW_LINE return copyHead NEW_LINE DEDENT def printList ( ptr ) : NEW_LINE INDENT while ( ptr != None ) : NEW_LINE INDENT print ( ptr . data , end = ' - > ' ) NEW_LINE ptr = ptr . next NEW_LINE DEDENT print ( ' NULL ' ) NEW_LINE DEDENT def newNode ( data ) : NEW_LINE INDENT temp = Node ( ) NEW_LINE temp . data = data NEW_LINE temp . next = None NEW_LINE return temp NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT 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 print ( " Gven ▁ Linked ▁ List " ) NEW_LINE printList ( head ) NEW_LINE head = deleteMid ( head ) NEW_LINE print ( " Linked ▁ List ▁ after ▁ deletion ▁ of ▁ middle " ) NEW_LINE printList ( head ) NEW_LINE DEDENT
Merge K sorted linked lists | Set 1 | A Linked List node ; Function to prnodes in a given linked list ; The main function that takes an array of lists arr [ 0. . last ] and generates the sorted output ; Traverse form second list to last ; head of both the lists , 0 and ith list . ; Break if list ended ; Smaller than first element ; Traverse the first list ; Smaller than next element ; go to next node ; if last node ; Driver code ; Number of linked lists ; Number of elements in each list ; an array of pointers storing the head nodes of the linked lists ; Merge all lists
class Node : NEW_LINE INDENT def __init__ ( self , x ) : NEW_LINE INDENT self . data = x NEW_LINE self . next = None NEW_LINE DEDENT 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 def mergeKLists ( arr , last ) : NEW_LINE INDENT for i in range ( 1 , last + 1 ) : NEW_LINE INDENT while ( True ) : NEW_LINE INDENT head_0 = arr [ 0 ] NEW_LINE head_i = arr [ i ] NEW_LINE if ( head_i == None ) : NEW_LINE INDENT break NEW_LINE DEDENT if ( head_0 . data >= head_i . data ) : NEW_LINE INDENT arr [ i ] = head_i . next NEW_LINE head_i . next = head_0 NEW_LINE arr [ 0 ] = head_i NEW_LINE DEDENT else : NEW_LINE INDENT while ( head_0 . next != None ) : NEW_LINE INDENT if ( head_0 . next . data >= head_i . data ) : NEW_LINE INDENT arr [ i ] = head_i . next NEW_LINE head_i . next = head_0 . next NEW_LINE head_0 . next = head_i NEW_LINE break NEW_LINE DEDENT head_0 = head_0 . next NEW_LINE if ( head_0 . next == None ) : NEW_LINE INDENT arr [ i ] = head_i . next NEW_LINE head_i . next = None NEW_LINE head_0 . next = head_i NEW_LINE head_0 . next . next = None NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT return arr [ 0 ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT k = 3 NEW_LINE n = 4 NEW_LINE arr = [ None for i in range ( k ) ] NEW_LINE arr [ 0 ] = Node ( 1 ) NEW_LINE arr [ 0 ] . next = Node ( 3 ) NEW_LINE arr [ 0 ] . next . next = Node ( 5 ) NEW_LINE arr [ 0 ] . next . next . next = Node ( 7 ) NEW_LINE arr [ 1 ] = Node ( 2 ) NEW_LINE arr [ 1 ] . next = Node ( 4 ) NEW_LINE arr [ 1 ] . next . next = Node ( 6 ) NEW_LINE arr [ 1 ] . next . next . next = Node ( 8 ) NEW_LINE arr [ 2 ] = Node ( 0 ) NEW_LINE arr [ 2 ] . next = Node ( 9 ) NEW_LINE arr [ 2 ] . next . next = Node ( 10 ) NEW_LINE arr [ 2 ] . next . next . next = Node ( 11 ) NEW_LINE head = mergeKLists ( arr , k - 1 ) NEW_LINE printList ( head ) NEW_LINE DEDENT
Merge two sorted lists ( in | Python3 program to merge two sorted linked lists in - place . ; Function to create newNode in a linkedlist ; A utility function to print linked list ; Merges two given lists in - place . This function mainly compares head nodes and calls mergeUtil ( ) ; start with the linked list whose head data is the least ; Driver Code ; 1.3 . 5 LinkedList created ; 0.2 . 4 LinkedList created
import math NEW_LINE 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 ( key ) : NEW_LINE INDENT temp = Node ( key ) NEW_LINE temp . data = key NEW_LINE temp . next = None NEW_LINE return temp 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 def merge ( h1 , h2 ) : NEW_LINE INDENT if ( h1 == None ) : NEW_LINE INDENT return h2 NEW_LINE DEDENT if ( h2 == None ) : NEW_LINE INDENT return h1 NEW_LINE DEDENT if ( h1 . data < h2 . data ) : NEW_LINE INDENT h1 . next = merge ( h1 . next , h2 ) NEW_LINE return h1 NEW_LINE DEDENT else : NEW_LINE INDENT h2 . next = merge ( h1 , h2 . next ) NEW_LINE return h2 NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head1 = newNode ( 1 ) NEW_LINE head1 . next = newNode ( 3 ) NEW_LINE head1 . next . next = newNode ( 5 ) NEW_LINE head2 = newNode ( 0 ) NEW_LINE head2 . next = newNode ( 2 ) NEW_LINE head2 . next . next = newNode ( 4 ) NEW_LINE mergedhead = merge ( head1 , head2 ) NEW_LINE printList ( mergedhead ) NEW_LINE DEDENT
Merge two sorted lists ( in | Linked List node ; Function to create newNode in a linkedlist ; A utility function to print linked list ; Merges two lists with headers as h1 and h2 . It assumes that h1 ' s ▁ data ▁ is ▁ smaller ▁ than ▁ or ▁ equal ▁ to ▁ h2' s data . ; if only one node in first list simply point its head to second list ; Initialize current and next pointers of both lists ; if curr2 lies in between curr1 and next1 then do curr1 . curr2 . next1 ; now let curr1 and curr2 to point to their immediate next pointers ; if more nodes in first list ; else point the last node of first list to the remaining nodes of second list ; Merges two given lists in - place . This function mainly compares head nodes and calls mergeUtil ( ) ; start with the linked list whose head data is the least ; Driver program ; 1.3 . 5 LinkedList created ; 0.2 . 4 LinkedList created
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 ( key ) : NEW_LINE INDENT temp = Node ( 0 ) NEW_LINE temp . data = key NEW_LINE temp . next = None NEW_LINE return temp 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 def mergeUtil ( h1 , h2 ) : NEW_LINE INDENT if ( h1 . next == None ) : NEW_LINE INDENT h1 . next = h2 NEW_LINE return h1 NEW_LINE DEDENT curr1 = h1 NEW_LINE next1 = h1 . next NEW_LINE curr2 = h2 NEW_LINE next2 = h2 . next NEW_LINE while ( next1 != None and curr2 != None ) : NEW_LINE INDENT if ( ( curr2 . data ) >= ( curr1 . data ) and ( curr2 . data ) <= ( next1 . data ) ) : NEW_LINE INDENT next2 = curr2 . next NEW_LINE curr1 . next = curr2 NEW_LINE curr2 . next = next1 NEW_LINE curr1 = curr2 NEW_LINE curr2 = next2 NEW_LINE DEDENT else : NEW_LINE INDENT if ( next1 . next ) : NEW_LINE INDENT next1 = next1 . next NEW_LINE curr1 = curr1 . next NEW_LINE DEDENT else : NEW_LINE INDENT next1 . next = curr2 NEW_LINE return h1 NEW_LINE DEDENT DEDENT DEDENT return h1 NEW_LINE DEDENT def merge ( h1 , h2 ) : NEW_LINE INDENT if ( h1 == None ) : NEW_LINE INDENT return h2 NEW_LINE DEDENT if ( h2 == None ) : NEW_LINE INDENT return h1 NEW_LINE DEDENT if ( h1 . data < h2 . data ) : NEW_LINE INDENT return mergeUtil ( h1 , h2 ) NEW_LINE DEDENT else : NEW_LINE INDENT return mergeUtil ( h2 , h1 ) NEW_LINE DEDENT DEDENT head1 = newNode ( 1 ) NEW_LINE head1 . next = newNode ( 3 ) NEW_LINE head1 . next . next = newNode ( 5 ) NEW_LINE head2 = newNode ( 0 ) NEW_LINE head2 . next = newNode ( 2 ) NEW_LINE head2 . next . next = newNode ( 4 ) NEW_LINE mergedhead = merge ( head1 , head2 ) NEW_LINE printList ( mergedhead ) NEW_LINE