codeparrot/xlcost-text-to-code huggingface dataset:
Partitioning a linked list around a given value and keeping the original order | Link list Node ; A utility function to create a new node ; Function to make two separate lists and return head after concatenating ; Let us initialize first and last nodes of three linked lists 1 ) Linked list of values smaller than x . 2 ) Linked list of values equal to x . 3 ) Linked list of values greater than x . ; Now iterate original list and connect nodes of appropriate linked lists . ; If current node is equal to x , append it to the list of x values ; If current node is less than X , append it to the list of smaller values ; Append to the list of greater values ; Fix end of greater linked list to NULL if this list has some nodes ; If smaller list is empty ; If smaller list is not empty and equal list is empty ; If both smaller and equal list are non - empty ; Function to prvar linked list ; Start with the empty list
class Node { constructor ( ) { this . data = 0 ; this . next = null ; } } function newNode ( data ) { var new_node = new Node ( ) ; new_node . data = data ; new_node . next = null ; return new_node ; } function partition ( head , x ) { var smallerHead = null , smallerLast = null ; var greaterLast = null , greaterHead = null ; var equalHead = null , equalLast = null ; while ( head != null ) { if ( head . data == x ) { if ( equalHead == null ) equalHead = equalLast = head ; else { equalLast . next = head ; equalLast = equalLast . next ; } } else if ( head . data < x ) { if ( smallerHead == null ) smallerLast = smallerHead = head ; else { smallerLast . next = head ; smallerLast = head ; } } else { if ( greaterHead == null ) greaterLast = greaterHead = head ; else { greaterLast . next = head ; greaterLast = head ; } } head = head . next ; } if ( greaterLast != null ) greaterLast . next = null ; if ( smallerHead == null ) { if ( equalHead == null ) return greaterHead ; equalLast . next = greaterHead ; return equalHead ; } if ( equalHead == null ) { smallerLast . next = greaterHead ; return smallerHead ; } smallerLast . next = equalHead ; equalLast . next = greaterHead ; return smallerHead ; } function printList ( head ) { var temp = head ; while ( temp != null ) { document . write ( temp . data + " " ) ; temp = temp . next ; } } var head = newNode ( 10 ) ; head . next = newNode ( 4 ) ; head . next . next = newNode ( 5 ) ; head . next . next . next = newNode ( 30 ) ; head . next . next . next . next = newNode ( 2 ) ; head . next . next . next . next . next = newNode ( 50 ) ; var x = 3 ; head = partition ( head , x ) ; printList ( head ) ;
Check linked list with a loop is palindrome or not | Link list node ; * Function to find loop starting node . loop_node -- > Pointer to one of the loop * nodes head -- > Pointer to the start node of the linked list ; Count the number of nodes in loop ; Fix one pointer to head ; And the other pointer to k nodes after head ; * Move both pointers at the same pace , they will meet at loop starting node ; * This function detects and find loop starting node in the list ; Start traversing list and detect loop ; * If slow_p and fast_p meet then find the loop starting node ; Return starting node of loop ; Utility function to check if a linked list with loop is palindrome with given starting point . ; Traverse linked list until last node is equal to loop_start and store the elements till start in a stack ; Traverse linked list until last node is equal to loop_start second time ; Compare data of node with the top of stack If equal then continue ; Else return false ; Return true if linked list is palindrome ; Function to find if linked list is palindrome or not ; Find the loop starting node ; Check if linked list is palindrome ; Driver code ; Create a loop for testing
class Node { constructor ( ) { this . data = 0 ; this . next = null ; } } function getLoopstart ( loop_node , head ) { var ptr1 = loop_node ; var ptr2 = loop_node ; var k = 1 , i ; while ( ptr1 . next != ptr2 ) { ptr1 = ptr1 . next ; k ++ ; } ptr1 = head ; ptr2 = head ; for ( i = 0 ; i < k ; i ++ ) ptr2 = ptr2 . next ; while ( ptr2 != ptr1 ) { ptr1 = ptr1 . next ; ptr2 = ptr2 . next ; } return ptr1 ; } function detectAndgetLoopstarting ( head ) { var slow_p = head , fast_p = head , loop_start = null ; while ( slow_p != null && fast_p != null && fast_p . next != null ) { slow_p = slow_p . next ; fast_p = fast_p . next . next ; if ( slow_p == fast_p ) { loop_start = getLoopstart ( slow_p , head ) ; break ; } } return loop_start ; } function isPalindromeUtil ( head , loop_start ) { var ptr = head ; var s = [ ] ; var count = 0 ; while ( ptr != loop_start count != 1 ) { s . push ( ptr . data ) ; if ( ptr == loop_start ) count = 1 ; ptr = ptr . next ; } ptr = head ; count = 0 ; while ( ptr != loop_start count != 1 ) { var stk = s . pop ( ) ; if ( ptr . data == stk ) ; else { s . push ( stk ) ; return false ; } if ( ptr == loop_start ) count = 1 ; ptr = ptr . next ; } return true ; } function isPalindrome ( head ) { var loop_start = detectAndgetLoopstarting ( head ) ; return isPalindromeUtil ( head , loop_start ) ; } function newNode ( key ) { var temp = new Node ( ) ; temp . data = key ; temp . next = null ; return temp ; } var head = newNode ( 50 ) ; head . next = newNode ( 20 ) ; head . next . next = newNode ( 15 ) ; head . next . next . next = newNode ( 20 ) ; head . next . next . next . next = newNode ( 50 ) ; head . next . next . next . next . next = head . next . next ; if ( isPalindrome ( head ) == true ) document . write ( " " ) ; else document . write ( " " ) ;
Length of longest palindrome list in a linked list using O ( 1 ) extra space | structure of the linked list ; 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 { constructor ( ) { this . data = 0 ; this . next = null ; } } function countCommon ( a , b ) { var count = 0 ; for ( ; a != null && b != null ; a = a . next , b = b . next ) if ( a . data == b . data ) ++ count ; else break ; return count ; } function maxPalindrome ( head ) { var result = 0 ; var prev = null , curr = head ; while ( curr != null ) { var next = curr . next ; curr . next = prev ; result = Math . max ( result , 2 * countCommon ( prev , next ) + 1 ) ; result = Math . max ( result , 2 * countCommon ( curr , next ) ) ; prev = curr ; curr = next ; } return result ; } function newNode ( key ) { var temp = new Node ( ) ; temp . data = key ; temp . next = null ; return temp ; } var head = newNode ( 2 ) ; head . next = newNode ( 4 ) ; head . next . next = newNode ( 3 ) ; head . next . next . next = newNode ( 4 ) ; head . next . next . next . next = newNode ( 2 ) ; head . next . next . next . next . next = newNode ( 15 ) ; document . write ( maxPalindrome ( head ) ) ;
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 .
var list = [ ] ; list . push ( 1 ) ; list . push ( 2 ) ; list . push ( 3 ) ; for ( var i = 0 ; i < list . length ; i ++ ) { document . write ( list [ i ] + " " ) ; }
Move all occurrences of an element to end in a linked list | A 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 { constructor ( ) { this . data = 0 ; this . next = null ; } } function newNode ( x ) { var temp = new Node ( ) ; temp . data = x ; temp . next = null ; return temp ; } function printList ( head ) { var temp = head ; while ( temp != null ) { document . write ( temp . data + " " ) ; temp = temp . next ; } document . write ( " " ) ; } function moveToEnd ( head , key ) { var pKey = head ; var pCrawl = head ; while ( pCrawl != null ) { if ( pCrawl != pKey && pCrawl . data != key ) { pKey . data = pCrawl . data ; pCrawl . data = key ; pKey = pKey . next ; } if ( pKey . data != key ) pKey = pKey . next ; pCrawl = pCrawl . next ; } } var head = newNode ( 10 ) ; head . next = newNode ( 20 ) ; head . next . next = newNode ( 10 ) ; head . next . next . next = newNode ( 30 ) ; head . next . next . next . next = newNode ( 40 ) ; head . next . next . next . next . next = newNode ( 10 ) ; head . next . next . next . next . next . next = newNode ( 60 ) ; document . write ( " " ) ; printList ( head ) ; var key = 10 ; moveToEnd ( head , key ) ; document . write ( " " ) ; printList ( head ) ;
Move all occurrences of an element to end in a linked list | Node class javascript code to remove key element to end of linked list ; Function to remove key to end ; Node to keep pointing to tail ; Node to povar to last of linked list ; Node prev2 to povar to previous when head . data != key ; loop to perform operations to remove key to end ; Function to display linked list ; Driver Code
class Node { constructor ( val ) { this . data = val ; this . next = null ; } } var root ; function keyToEnd ( head , key ) { var tail = head ; if ( head == null ) { return null ; } while ( tail . next != null ) { tail = tail . next ; } var last = tail ; var current = head ; var prev = null ; var prev2 = null ; while ( current != tail ) { if ( current . data == key && prev2 == null ) { prev = current ; current = current . next ; head = current ; last . next = prev ; last = last . next ; last . next = null ; prev = null ; } else { if ( current . data == key && prev2 != null ) { prev = current ; current = current . next ; prev2 . next = current ; last . next = prev ; last = last . next ; last . next = null ; } else if ( current != tail ) { prev2 = current ; current = current . next ; } } } return head ; } function display ( root ) { while ( root != null ) { document . write ( root . data + " " ) ; root = root . next ; } } root = new Node ( 5 ) ; root . next = new Node ( 2 ) ; root . next . next = new Node ( 2 ) ; root . next . next . next = new Node ( 7 ) ; root . next . next . next . next = new Node ( 2 ) ; root . next . next . next . next . next = new Node ( 2 ) ; root . next . next . next . next . next . next = new Node ( 2 ) ; var key = 2 ; document . write ( " " ) ; display ( root ) ; document . write ( " " ) ; root = keyToEnd ( root , key ) ; display ( root ) ;
Remove all occurrences of duplicates from a sorted Linked List | class to create Linked lIst ; Function to insert data nodes into the Linked List at the front ; Function to remove all occurrences of duplicate elements ; Create a dummy node that acts like a fake head of list pointing to the original head ; Dummy node points to the original head ; Until the current and previous values are same , keep updating current ; If current has unique value i . e current is not updated , Move the prev pointer to next node ; When current is updated to the last duplicate value of that segment , make prev the next of current ; Update original head to the next of dummy node ; Function to print the list elements ; Driver code
var head = null ; class Node { constructor ( val ) { this . val = val ; this . next = null ; } } function insert ( data ) { var new_node = new Node ( data ) ; new_node . next = head ; head = new_node ; } function removeAllDuplicates ( ) { var dummy = new Node ( 0 ) ; dummy . next = head ; var prev = dummy ; var current = head ; while ( current != null ) { while ( current . next != null && prev . next . val == current . next . val ) current = current . next ; if ( prev . next == current ) prev = prev . next ; else prev . next = current . next ; current = current . next ; } head = dummy . next ; } function printList ( ) { var trav = head ; if ( head == null ) document . write ( " " ) ; while ( trav != null ) { document . write ( trav . val + " " ) ; trav = trav . next ; } } insert ( 53 ) ; insert ( 53 ) ; insert ( 49 ) ; insert ( 49 ) ; insert ( 35 ) ; insert ( 28 ) ; insert ( 28 ) ; insert ( 23 ) ; document . write ( " " ) ; printList ( ) ; removeAllDuplicates ( ) ; document . write ( " " ) ; printList ( ) ;
Remove every k | Linked list Node ; To remove complete list ( Needed for case 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 ; set count = 0 to reach further k - th Node ; update prev if count is not 0 ; Function to prvar linked list ; Utility function to create a new node . ; Start with the empty list
class Node { constructor ( ) { this . data = 0 ; this . next = null ; } } function freeList ( node ) { while ( node != null ) { next = node . next ; node = next ; } return node ; } function deleteKthNode ( head , k ) { if ( head == null ) return null ; if ( k == 1 ) { head = freeList ( head ) ; return null ; } var ptr = head , prev = null ; var count = 0 ; while ( ptr != null ) { count ++ ; if ( k == count ) { prev . next = ptr . next ; count = 0 ; } if ( count != 0 ) prev = ptr ; ptr = prev . next ; } return head ; } function displayList ( head ) { temp = head ; while ( temp != null ) { document . write ( temp . data + " " ) ; temp = temp . next ; } } function newNode ( x ) { temp = new Node ( ) ; temp . data = x ; temp . next = null ; return temp ; } head = newNode ( 1 ) ; head . next = newNode ( 2 ) ; head . next . next = newNode ( 3 ) ; head . next . next . next = newNode ( 4 ) ; head . next . next . next . next = newNode ( 5 ) ; head . next . next . next . next . next = newNode ( 6 ) ; head . next . next . next . next . next . next = newNode ( 7 ) ; head . next . next . next . next . next . next . next = newNode ( 8 ) ; var k = 3 ; head = deleteKthNode ( head , k ) ; displayList ( head ) ;
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 ; Info into 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 { constructor ( ) { this . data = 0 ; this . next = null ; } } function LinkedListLength ( head ) { while ( head != null && head . next != null ) { head = head . next . next ; } if ( head == null ) return 0 ; return 1 ; } function push ( head , info ) { node = new Node ( ) ; node . data = info ; node . next = ( head ) ; ( head ) = node ; } head = null ; push ( head , 4 ) ; push ( head , 5 ) ; push ( head , 7 ) ; push ( head , 2 ) ; push ( head , 9 ) ; push ( head , 6 ) ; push ( head , 1 ) ; push ( head , 2 ) ; push ( head , 0 ) ; push ( head , 5 ) ; push ( head , 5 ) ; var check = LinkedListLength ( head ) ; if ( check == 0 ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
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 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 program to test above ; create linked list 10.6 .8 .4 .12
class Node { constructor ( ) { let data , next ; } } function push ( head_ref , new_data ) { let new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; return head_ref ; } function sumOfLastN_NodesUtil ( head , n ) { if ( n <= 0 ) return 0 ; let st = [ ] ; let sum = 0 ; while ( head != null ) { st . push ( head . data ) ; head = head . next ; } while ( n -- > 0 ) { sum += st [ st . length - 1 ] ; st . pop ( ) ; } return sum ; } let head = null ; head = push ( head , 12 ) ; head = push ( head , 4 ) ; head = push ( head , 8 ) ; head = push ( head , 6 ) ; head = push ( head , 10 ) ; let n = 2 ; document . write ( " " + n + " " + sumOfLastN_NodesUtil ( head , n ) ) ;
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 povar to 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 ; create linked list 10.6 .8 .4 .12
class Node { constructor ( ) { this . data = 0 ; this . next = null ; } } var head ; 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 ; head = head_ref ; } function reverseList ( head_ref ) { var current , prev , next ; current = head_ref ; prev = null ; while ( current != null ) { next = current . next ; current . next = prev ; prev = current ; current = next ; } head_ref = prev ; head = head_ref ; } function sumOfLastN_NodesUtil ( n ) { if ( n <= 0 ) return 0 ; reverseList ( head ) ; var sum = 0 ; var current = head ; while ( current != null && n -- > 0 ) { sum += current . data ; current = current . next ; } reverseList ( head ) ; return sum ; } push ( head , 12 ) ; push ( head , 4 ) ; push ( head , 8 ) ; push ( head , 6 ) ; push ( head , 10 ) ; var n = 2 ; document . write ( " " + n + " " + sumOfLastN_NodesUtil ( n ) ) ;
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 point to 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 ; create linked list 10.6 .8 .4 .12
class Node { constructor ( ) { this . data = 0 ; this . next = null ; } } var head ; function push ( head_ref , new_data ) { new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; head = head_ref ; } function sumOfLastN_NodesUtil ( head , n ) { if ( n <= 0 ) return 0 ; var sum = 0 , len = 0 ; temp = head ; while ( temp != null ) { len ++ ; temp = temp . next ; } var c = len - n ; temp = head ; while ( temp != null && c -- > 0 ) { temp = temp . next ; } while ( temp != null ) { sum += temp . data ; temp = temp . next ; } return sum ; } push ( head , 12 ) ; push ( head , 4 ) ; push ( head , 8 ) ; push ( head , 6 ) ; push ( head , 10 ) ; var n = 2 ; document . write ( " " + n + " " + sumOfLastN_NodesUtil ( head , n ) ) ;
Merge two sorted linked lists |  ; point to the last result pointer ; tricky : advance to point to the next " . next " field
function SortedMerge ( a , b ) { let result = null ; let lastPtrRef = result ; while ( 1 ) { if ( a == null ) { lastPtrRef = b ; break ; } else if ( b == null ) { lastPtrRef = a ; break ; } if ( a . data <= b . data ) { MoveNode ( lastPtrRef , a ) ; } else { MoveNode ( lastPtrRef , b ) ; } lastPtrRef = ( ( lastPtrRef ) . next ) ; } return ( result ) ; }
Merge K sorted linked lists | Set 1 | A Linked List node ; Utility function to create a new node . ; Function to print nodes 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 ; 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 { constructor ( key ) { this . data = key ; this . next = null ; } } let head ; let temp ; function printList ( node ) { while ( node != null ) { document . write ( node . data + " " ) ; node = node . next ; } document . write ( " " ) ; } function mergeKLists ( arr , last ) { for ( let i = 1 ; i <= last ; i ++ ) { while ( true ) { let head_0 = arr [ 0 ] ; let head_i = arr [ i ] ; if ( head_i == null ) break ; if ( head_0 . data >= head_i . data ) { arr [ i ] = head_i . next ; head_i . next = head_0 ; arr [ 0 ] = head_i ; } else { while ( head_0 . next != null ) { if ( head_0 . next . data >= head_i . data ) { arr [ i ] = head_i . next ; head_i . next = head_0 . next ; head_0 . next = head_i ; break ; } head_0 = head_0 . next ; if ( head_0 . next == null ) { arr [ i ] = head_i . next ; head_i . next = null ; head_0 . next = head_i ; head_0 . next . next = null ; break ; } } } } } return arr [ 0 ] ; } let k = 3 ; let n = 4 ; let arr = new Array ( k ) ; arr [ 0 ] = new Node ( 1 ) ; arr [ 0 ] . next = new Node ( 3 ) ; arr [ 0 ] . next . next = new Node ( 5 ) ; arr [ 0 ] . next . next . next = new Node ( 7 ) ; arr [ 1 ] = new Node ( 2 ) ; arr [ 1 ] . next = new Node ( 4 ) ; arr [ 1 ] . next . next = new Node ( 6 ) ; arr [ 1 ] . next . next . next = new Node ( 8 ) ; arr [ 2 ] = new Node ( 0 ) ; arr [ 2 ] . next = new Node ( 9 ) ; arr [ 2 ] . next . next = new Node ( 10 ) ; arr [ 2 ] . next . next . next = new Node ( 11 ) ; head = mergeKLists ( arr , k - 1 ) ; printList ( head ) ;
Merge two sorted lists ( in | javascript program to merge two sorted linked lists in - place . ; Function to create newNode in a linkedlist ; A utility function to prvar 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 program ; 1.3 . 5 LinkedList created ; 0.2 . 4 LinkedList created
class Node { constructor ( ) { this . data = 0 ; this . next = null ; } } function newNode ( key ) { 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 merge ( h1 , h2 ) { if ( h1 == null ) return h2 ; if ( h2 == null ) return h1 ; if ( h1 . data < h2 . data ) { h1 . next = merge ( h1 . next , h2 ) ; return h1 ; } else { h2 . next = merge ( h1 , h2 . next ) ; return h2 ; } } head1 = newNode ( 1 ) ; head1 . next = newNode ( 3 ) ; head1 . next . next = newNode ( 5 ) ; head2 = newNode ( 0 ) ; head2 . next = newNode ( 2 ) ; head2 . next . next = newNode ( 4 ) ; mergedhead = merge ( head1 , head2 ) ; printList ( mergedhead ) ;