codeparrot/xlcost-text-to-code huggingface dataset:
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 povar 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 code ; 1 -> 3 -> 5 LinkedList created ; 0 -> 2 -> 4 LinkedList created
class Node { constructor ( ) { this . data = 0 ; this . next = null ; } } function newNode ( key ) { var temp = new Node ( ) ; temp . data = key ; temp . next = null ; return temp ; } function printList ( node ) { while ( node != null ) { document . write ( node . data + " " ) ; node = node . next ; } } function mergeUtil ( h1 , h2 ) { if ( h1 . next == null ) { h1 . next = h2 ; return h1 ; } var curr1 = h1 , next1 = h1 . next ; var curr2 = h2 , next2 = h2 . next ; while ( next1 != null && curr2 != null ) { if ( ( curr2 . data ) >= ( curr1 . data ) && ( curr2 . data ) <= ( next1 . data ) ) { next2 = curr2 . next ; curr1 . next = curr2 ; curr2 . next = next1 ; curr1 = curr2 ; curr2 = next2 ; } else { if ( next1 . next != null ) { next1 = next1 . next ; curr1 = curr1 . next ; } else { next1 . next = curr2 ; return h1 ; } } } return h1 ; } function merge ( h1 , h2 ) { if ( h1 == null ) return h2 ; if ( h2 == null ) return h1 ; if ( h1 . data < h2 . data ) return mergeUtil ( h1 , h2 ) ; else return mergeUtil ( h2 , h1 ) ; } var head1 = newNode ( 1 ) ; head1 . next = newNode ( 3 ) ; head1 . next . next = newNode ( 5 ) ; var head2 = newNode ( 0 ) ; head2 . next = newNode ( 2 ) ; head2 . next . next = newNode ( 4 ) ; var mergedhead = merge ( head1 , head2 ) ; printList ( mergedhead ) ;
Recursive selection sort for singly linked list | Swapping node links | A Linked list node ; function to swap nodes ' currX ' and ' currY ' in a linked list without swapping data ; make ' currY ' as new head ; adjust links ; Swap next pointers ; function to sort the linked list using recursive selection sort technique ; if there is only a single node ; ' min ' - pointer to store the node having minimum data value ; ' beforeMin ' - pointer to store node previous to ' min ' node ; traverse the list till the last node ; if true , then update ' min ' and ' beforeMin ' ; if ' min ' and ' head ' are not same , swap the head node with the ' min ' node ; recursively sort the remaining list ; function to sort the given linked list ; if list is empty ; sort the list using recursive selection sort technique ; 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 povar to the new node ; function to prvar the linked list ; Driver code ; create linked list 10.12 .8 .4 .6 ; sort the linked list
class Node { constructor ( val ) { this . data = val ; this . next = null ; } } function swapNodes ( head_ref , currX , currY , prevY ) { head_ref = currY ; prevY . next = currX ; var temp = currY . next ; currY . next = currX . next ; currX . next = temp ; return head_ref ; } function recurSelectionSort ( head ) { if ( head . next == null ) return head ; var min = head ; var beforeMin = null ; var ptr ; for ( ptr = head ; ptr . next != null ; ptr = ptr . next ) { if ( ptr . next . data < min . data ) { min = ptr . next ; beforeMin = ptr ; } } if ( min != head ) head = swapNodes ( head , head , min , beforeMin ) ; head . next = recurSelectionSort ( head . next ) ; return head ; } function sort ( head_ref ) { if ( ( head_ref ) == null ) return null ; head_ref = recurSelectionSort ( head_ref ) ; return head_ref ; } function push ( head_ref , new_data ) { var new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = ( head_ref ) ; ( head_ref ) = new_node ; return head_ref ; } function printList ( head ) { while ( head != null ) { document . write ( head . data + " " ) ; head = head . next ; } } var head = null ; head = push ( head , 6 ) ; head = push ( head , 4 ) ; head = push ( head , 8 ) ; head = push ( head , 12 ) ; head = push ( head , 10 ) ; document . write ( " " ) ; printList ( head ) ; head = sort ( head ) ; document . write ( " " ) ; printList ( head ) ;
Insert a node after the n | structure of a node ; function to get a new node ; allocate memory for the node ; put in the data ; function to insert a node after the nth node from the end ; if list is empty ; get a new node for the value ' x ' ; find length of the list , i . e , the number of nodes in the list ; traverse up to the nth node from the end ; insert the ' newNode ' by making the necessary adjustment in the links ; function to print the list ; Creating list 1 -> 3 -> 4 -> 5
class Node { constructor ( ) { this . data = 0 ; this . next = null ; } } function getNode ( data ) { var newNode = new Node ( ) ; newNode . data = data ; newNode . next = null ; return newNode ; } function insertAfterNthNode ( head , n , x ) { if ( head == null ) return ; var newNode = getNode ( x ) ; var ptr = head ; var len = 0 , i ; while ( ptr != null ) { len ++ ; ptr = ptr . next ; } ptr = head ; for ( i = 1 ; i <= ( len - n ) ; i ++ ) ptr = ptr . next ; newNode . next = ptr . next ; ptr . next = newNode ; } function printList ( head ) { while ( head != null ) { document . write ( head . data + " " ) ; head = head . next ; } } var head = getNode ( 1 ) ; head . next = getNode ( 3 ) ; head . next . next = getNode ( 4 ) ; head . next . next . next = getNode ( 5 ) ; var n = 4 , x = 2 ; document . write ( " " ) ; printList ( head ) ; insertAfterNthNode ( head , n , x ) ; document . write ( ) ; document . write ( " " ) ; printList ( head ) ;
Insert a node after the n | structure of a node ; function to get a new node ; allocate memory for the node ; put in the data ; function to insert a node after the nth node from the end ; if list is empty ; get a new node for the value ' x ' ; Initializing the slow and fast pointers ; move ' fast _ ptr ' to point to the nth node from the beginning ; iterate until ' fast _ ptr ' points to the last node ; move both the pointers to the respective next nodes ; insert the ' newNode ' by making the necessary adjustment in the links ; function to print the list ; Creating list 1 -> 3 -> 4 -> 5
class Node { constructor ( ) { this . data = 0 ; this . next = null ; } } function getNode ( data ) { var newNode = new Node ( ) ; newNode . data = data ; newNode . next = null ; return newNode ; } function insertAfterNthNode ( head , n , x ) { if ( head == null ) return ; var newNode = getNode ( x ) ; var slow_ptr = head ; var fast_ptr = head ; for ( var i = 1 ; i <= n - 1 ; i ++ ) fast_ptr = fast_ptr . next ; while ( fast_ptr . next != null ) { slow_ptr = slow_ptr . next ; fast_ptr = fast_ptr . next ; } newNode . next = slow_ptr . next ; slow_ptr . next = newNode ; } function printList ( head ) { while ( head != null ) { document . write ( head . data + " " ) ; head = head . next ; } } var head = getNode ( 1 ) ; head . next = getNode ( 3 ) ; head . next . next = getNode ( 4 ) ; head . next . next . next = getNode ( 5 ) ; var n = 4 , x = 2 ; document . write ( " " ) ; printList ( head ) ; insertAfterNthNode ( head , n , x ) ; document . write ( " " ) ; document . write ( " " ) ; printList ( head ) ;
Make middle node head in a linked list | Link list node ; * Function to get the middle and set at beginning of the linked list ; To traverse list nodes one by one ; To traverse list nodes by skipping one . ; To keep track of previous of middle ; for previous node of middle node ; move one node each time ; move two node each time ; set middle node at head ; To insert a node at the beginning of linked list . ; allocate node ; link the old list off the new node ; move the head to povar to the new node ; A function to prvar a given linked list ; Create a list of 5 nodes
class Node { constructor ( val ) { this . data = val ; this . next = null ; } } var head ; function setMiddleHead ( ) { if ( head == null ) return ; one_node = head ; two_node = head ; prev = null ; while ( two_node != null && two_node . next != null ) { prev = one_node ; two_node = two_node . next . next ; one_node = one_node . next ; } prev . next = prev . next . next ; one_node . next = head ; head = one_node ; } function push ( new_data ) { new_node = new Node ( new_data ) ; new_node . next = head ; head = new_node ; } function printList ( ptr ) { while ( ptr != null ) { document . write ( ptr . data + " " ) ; ptr = ptr . next ; } document . write ( " " ) ; } head = null ; var i ; for ( i = 5 ; i > 0 ; i -- ) push ( i ) ; document . write ( " " ) ; printList ( head ) ; setMiddleHead ( ) ; document . write ( " " ) ; printList ( head ) ;
Doubly Linked List | Set 1 ( Introduction and Insertion ) |  ; 1. check if the given prev_node is NULL ; 2. allocate node * 3. put in the data ; 4. Make next of new node as next of prev_node ; 5. Make the next of prev_node as new_node ; 6. Make prev_node as previous of new_node ; 7. Change previous of new_node 's next node
function InsertAfter ( prev_Node , new_data ) { if ( prev_Node == null ) { document . write ( " " ) ; return ; } let new_node = new Node ( new_data ) ; new_node . next = prev_Node . next ; prev_Node . next = new_node ; new_node . prev = prev_Node ; if ( new_node . next != null ) new_node . next . prev = new_node ; }
Sorted merge of two sorted doubly circular linked lists | javascript implementation for Sorted merge of two sorted doubly circular linked list ; A utility function to insert a new node at the beginning of doubly circular linked list ; allocate space ; put in the data ; if list is empty ; pointer points to last Node ; setting up previous and next of new node ; update next and previous pointers of head_ref and last . ; update head_ref pointer ; function for Sorted merge of two sorted doubly linked list ; If first list is empty ; If second list is empty ; Pick the smaller value and adjust the links ; function for Sorted merge of two sorted doubly circular linked list ; if 1 st list is empty ; if 2 nd list is empty ; get pointer to the node which will be the last node of the final list ; store null to the ' next ' link of the last nodes of the two lists ; sorted merge of head1 and head2 ; ' prev ' of 1 st node pointing the last node ' next ' of last node pointing to 1 st node ; function to prvar the list ; Driver code ; list 1 : ; list 2 :
class Node { constructor ( ) { this . data = 0 ; this . next = this . prev = null ; } } function insert ( head_ref , data ) { new_node = new Node ( ) ; new_node . data = data ; if ( head_ref == null ) { new_node . next = new_node ; new_node . prev = new_node ; } else { last = ( head_ref ) . prev ; new_node . next = head_ref ; new_node . prev = last ; last . next = ( head_ref ) . prev = new_node ; } head_ref = new_node ; return head_ref ; } function merge ( first , second ) { if ( first == null ) return second ; if ( second == null ) return first ; if ( first . data < second . data ) { first . next = merge ( first . next , second ) ; first . next . prev = first ; first . prev = null ; return first ; } else { second . next = merge ( first , second . next ) ; second . next . prev = second ; second . prev = null ; return second ; } } function mergeUtil ( head1 , head2 ) { if ( head1 == null ) return head2 ; if ( head2 == null ) return head1 ; last_node = null ; if ( head1 . prev . data < head2 . prev . data ) last_node = head2 . prev ; else last_node = head1 . prev ; head1 . prev . next = head2 . prev . next = null ; finalHead = merge ( head1 , head2 ) ; finalHead . prev = last_node ; last_node . next = finalHead ; return finalHead ; } function printList ( head ) { temp = head ; while ( temp . next != head ) { document . write ( temp . data + " " ) ; temp = temp . next ; } document . write ( temp . data + " " ) ; } head1 = null , head2 = null ; head1 = insert ( head1 , 8 ) ; head1 = insert ( head1 , 5 ) ; head1 = insert ( head1 , 3 ) ; head1 = insert ( head1 , 1 ) ; head2 = insert ( head2 , 11 ) ; head2 = insert ( head2 , 9 ) ; head2 = insert ( head2 , 7 ) ; head2 = insert ( head2 , 2 ) ; newHead = mergeUtil ( head1 , head2 ) ; document . write ( " " ) ; printList ( newHead ) ;
Reverse a Doubly Linked List | Set 4 ( Swapping Data ) | javascript Program to Reverse a List using Data Swapping ; Insert a new node at the head of the list ; Function to reverse the list ; Traverse the list and set right pointer to end of list ; Swap data of left and right pointer and move them towards each other until they meet or cross each other ; Swap data of left and right pointer ; Advance left pointer ; Advance right pointer ; Driver code
class Node { constructor ( val ) { this . data = val ; this . prev = null ; this . next = null ; } } function newNode ( val ) { var temp = new Node ( ) ; temp . data = val ; temp . prev = temp . next = null ; return temp ; } function printList ( head ) { while ( head . next != null ) { document . write ( head . data + " " ) ; head = head . next ; } document . write ( head . data ) ; } function insert ( head , val ) { var temp = newNode ( val ) ; temp . next = head ; ( head ) . prev = temp ; ( head ) = temp ; return head ; } function reverseList ( head ) { var left = head , right = head ; while ( right . next != null ) right = right . next ; while ( left != right && left . prev != right ) { var t = left . data ; left . data = right . data ; right . data = t ; left = left . next ; right = right . prev ; } return head ; } var head = newNode ( 5 ) ; head = insert ( head , 4 ) ; head = insert ( head , 3 ) ; head = insert ( head , 2 ) ; head = insert ( head , 1 ) ; printList ( head ) ; document . write ( " " ) ; head = reverseList ( head ) ; printList ( head ) ;
Check if a doubly linked list of characters is palindrome or not | Structure of node ; * Given a reference ( pointer to pointer ) to the head of a list and an int , * inserts a new node on the front of the list . ; Function to check if list is palindrome or not ; Find rightmost node ; Driver program
class Node { constructor ( ) { this . data = 0 ; this . prev = null ; this . next = null ; } } function push ( head_ref , new_data ) { var new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; new_node . prev = null ; if ( head_ref != null ) head_ref . prev = new_node ; head_ref = new_node ; return head_ref ; } function isPalindrome ( left ) { if ( left == null ) return true ; var right = left ; while ( right . next != null ) right = right . next ; while ( left != right ) { if ( left . data != right . data ) return false ; left = left . next ; right = right . prev ; } return true ; } var head = null ; head = push ( head , ' ' ) ; head = push ( head , ' ' ) ; head = push ( head , ' ' ) ; head = push ( head , ' ' ) ; head = push ( head , ' ' ) ; if ( isPalindrome ( head ) ) document . write ( " " ) ; else document . write ( " " ) ;
Print nodes between two given level numbers of a binary tree | A binary tree Node has key , pointer to left and right children ; Given a binary tree , print nodes from level number ' low ' to level number ' high ' ; Marker node to indicate end of level ; Initialize level number ; Enqueue the only first level node and marker node for end of level ; Simple level order traversal loop ; Remove the front item from queue ; Check if end of level is reached ; print a new line and increment level number ; Check if marker node was last node in queue or level number is beyond the given upper limit ; Enqueue the marker for end of next level ; If this is marker , then we don 't need print it  and enqueue its children ; If level is equal to or greater than given lower level , print it ; Enqueue children of non - marker node ; Driver program to test for above functions
class Node { constructor ( item ) { this . data = item ; this . left = null ; this . right = null ; } } var root = null ; function printLevels ( node , low , high ) { var Q = [ ] ; var marker = new Node ( 4 ) ; var level = 1 ; Q . push ( node ) ; Q . push ( marker ) ; while ( Q . length > 0 ) { var n = Q [ 0 ] ; Q . shift ( ) ; if ( n == marker ) { document . write ( " " ) ; level ++ ; if ( Q . length == 0 level > high ) { break ; } Q . push ( marker ) ; continue ; } if ( level >= low ) { document . write ( n . data + " " ) ; } if ( n . left != null ) { Q . push ( n . left ) ; } if ( n . right != null ) { Q . push ( n . right ) ; } } } root = new Node ( 20 ) ; root . left = new Node ( 8 ) ; root . right = new Node ( 22 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 12 ) ; root . left . right . left = new Node ( 10 ) ; root . left . right . right = new Node ( 14 ) ; document . write ( " " ) ; printLevels ( root , 2 , 3 ) ;
Print nodes at k distance from root | A binary tree node has data , pointer to left child and a pointer to right child ; Constructed binary tree is 1 / \ 2 3 / \ / 4 5 8
class Node { constructor ( item ) { this . data = item ; this . left = null ; this . right = null ; } } var root = null ; function printKDistant ( node , k ) { if ( node == null k < 0 ) { return ; } if ( k == 0 ) { document . write ( node . data + " " ) ; return ; } printKDistant ( node . left , k - 1 ) ; printKDistant ( node . right , k - 1 ) ; } root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; root . right . left = new Node ( 8 ) ; printKDistant ( root , 2 ) ;
Print nodes at k distance from root | Iterative | Node of binary tree ; Function to add a new node ; Function to print nodes of given level ; extra null is added to keep track of all the nodes to be added before level is incremented by 1 ; print when level is equal to k ; break the loop if level exceeds the given level number ; create a binary tree
class Node { constructor ( data ) { this . left = null ; this . right = null ; this . data = data ; } } function newNode ( data ) { let newnode = new Node ( data ) ; return newnode ; } function printKDistant ( root , klevel ) { let q = [ ] ; let level = 1 ; let flag = false ; q . push ( root ) ; q . push ( null ) ; while ( q . length > 0 ) { let temp = q [ 0 ] ; if ( level == klevel && temp != null ) { flag = true ; document . write ( temp . data + " " ) ; } q . shift ( ) ; if ( temp == null ) { if ( q [ 0 ] != null ) q . push ( null ) ; level += 1 ; if ( level > klevel ) break ; } else { if ( temp . left != null ) q . push ( temp . left ) ; if ( temp . right != null ) q . push ( temp . right ) ; } } document . write ( " " ) ; return flag ; } let root = newNode ( 20 ) ; root . left = newNode ( 10 ) ; root . right = newNode ( 30 ) ; root . left . left = newNode ( 5 ) ; root . left . right = newNode ( 15 ) ; root . left . right . left = newNode ( 12 ) ; root . right . left = newNode ( 25 ) ; root . right . right = newNode ( 40 ) ; document . write ( " " ) ; let ret = printKDistant ( root , 1 ) ; if ( ret == false ) document . write ( " " + " " + " " ) ; document . write ( " " ) ; ret = printKDistant ( root , 2 ) ; if ( ret == false ) document . write ( " " + " " + " " ) ; document . write ( " " ) ; ret = printKDistant ( root , 3 ) ; if ( ret == false ) document . write ( " " + " " + " " ) ; document . write ( " " ) ; ret = printKDistant ( root , 6 ) ; if ( ret == false ) document . write ( " " + " " + " " ) ;
Print all leaf nodes of a Binary Tree from left to right | A Binary Tree Node ; Function to print leaf nodes from left to right ; If node is null , return ; If node is leaf node , print its data ; If left child exists , check for leaf recursively ; If right child exists , check for leaf recursively ; Utility function to create a new tree node ; Let us create binary tree shown in above diagram ; Print leaf nodes of the given tree
class Node { constructor ( ) { this . data = 0 ; this . left = null ; this . right = null ; } } ; function printLeafNodes ( root ) { if ( root == null ) return ; if ( root . left == null && root . right == null ) { document . write ( root . data + " " ) ; return ; } if ( root . left != null ) printLeafNodes ( root . left ) ; if ( root . right != null ) printLeafNodes ( root . right ) ; } function newNode ( data ) { var temp = new Node ( ) ; temp . data = data ; temp . left = null ; temp . right = null ; return temp ; } var root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . right . left = newNode ( 5 ) ; root . right . right = newNode ( 8 ) ; root . right . left . left = newNode ( 6 ) ; root . right . left . right = newNode ( 7 ) ; root . right . right . left = newNode ( 9 ) ; root . right . right . right = newNode ( 10 ) ; printLeafNodes ( root ) ;
Print all nodes at distance k from a given node | A binary tree node ; Recursive function to print all the nodes at distance k in tree ( or subtree ) rooted with given root . ; Base Case ; If we reach a k distant node , print it ; Recur for left and right subtrees ; Prints all nodes at distance k from a given target node . The k distant nodes may be upward or downward . This function Returns distance of root from target node , it returns - 1 if target node is not present in tree rooted with root . ; Base Case 1 : If tree is empty , return - 1 ; If target is same as root . Use the downward function to print all nodes at distance k in subtree rooted with target or root ; Recur for left subtree ; Check if target node was found in left subtree ; If root is at distance k from target , print root Note that dl is Distance of root 's left child from   target ; Else go to right subtree and print all k - dl - 2 distant nodes Note that the right child is 2 edges away from left child ; Add 1 to the distance and return value for parent calls ; MIRROR OF ABOVE CODE FOR RIGHT SUBTREE Note that we reach here only when node was not found in left subtree ; If target was neither present in left nor in right subtree ; Let us construct the tree shown in above diagram
class Node { constructor ( item ) { this . data = item ; this . left = null ; this . right = null ; } } var root = null ; function printkdistanceNodeDown ( node , k ) { if ( node == null k < 0 ) { return ; } if ( k == 0 ) { document . write ( node . data ) ; document . write ( " " ) ; return ; } printkdistanceNodeDown ( node . left , k - 1 ) ; printkdistanceNodeDown ( node . right , k - 1 ) ; } function printkdistanceNode ( node , target , k ) { if ( node == null ) { return - 1 ; } if ( node == target ) { printkdistanceNodeDown ( node , k ) ; return 0 ; } var dl = printkdistanceNode ( node . left , target , k ) ; if ( dl != - 1 ) { if ( dl + 1 == k ) { document . write ( node . data ) ; document . write ( " " ) ; } else { printkdistanceNodeDown ( node . right , k - dl - 2 ) ; } return 1 + dl ; } var dr = printkdistanceNode ( node . right , target , k ) ; if ( dr != - 1 ) { if ( dr + 1 == k ) { document . write ( node . data ) ; document . write ( " " ) ; } else { printkdistanceNodeDown ( node . left , k - dr - 2 ) ; } return 1 + dr ; } return - 1 ; } root = new Node ( 20 ) ; root . left = new Node ( 8 ) ; root . right = new Node ( 22 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 12 ) ; root . left . right . left = new Node ( 10 ) ; root . left . right . right = new Node ( 14 ) ; var target = root . left . right ; printkdistanceNode ( root , target , 2 ) ;