codeparrot/xlcost-text-to-code huggingface dataset:
Point arbit pointer to greatest value right side node in a linked list | Java program to point arbit pointers to highest value on its right ; Link list node ; 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 GfG { static class Node { int data ; Node next , arbit ; } static Node reverse ( Node head ) { Node prev = null , current = head , next = null ; while ( current != null ) { next = current . next ; current . next = prev ; prev = current ; current = next ; } return prev ; } static Node populateArbit ( Node head ) { head = reverse ( head ) ; Node max = head ; Node temp = head . next ; while ( temp != null ) { temp . arbit = max ; if ( max . data < temp . data ) max = temp ; temp = temp . next ; } return reverse ( head ) ; } static void printNextArbitPointers ( Node node ) { System . out . println ( " Node TABSYMBOL Next ▁ Pointer TABSYMBOL Arbit ▁ Pointer " ) ; while ( node != null ) { System . out . print ( node . data + " TABSYMBOL TABSYMBOL " ) ; if ( node . next != null ) System . out . print ( node . next . data + " TABSYMBOL TABSYMBOL " ) ; else System . out . print ( " NULL " + " TABSYMBOL TABSYMBOL " ) ; if ( node . arbit != null ) System . out . print ( node . arbit . data ) ; else System . out . print ( " NULL " ) ; System . out . println ( ) ; node = node . next ; } } static Node newNode ( int data ) { Node new_node = new Node ( ) ; new_node . data = data ; new_node . next = null ; return new_node ; } public static void main ( String [ ] args ) { Node head = newNode ( 5 ) ; head . next = newNode ( 10 ) ; head . next . next = newNode ( 2 ) ; head . next . next . next = newNode ( 3 ) ; head = populateArbit ( head ) ; System . out . println ( " Resultant ▁ Linked ▁ List ▁ is : ▁ " ) ; printNextArbitPointers ( head ) ; } }
Point arbit pointer to greatest value right side node in a linked list | Java program to point arbit pointers to highest value on its right ; Link list node ; This function populates arbit pointer in every node to the greatest value to its right . ; 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 print result linked list ; Function to create a new node with given data ; Driver code
class GfG { static class Node { int data ; Node next , arbit ; } static Node maxNode ; static void populateArbit ( Node head ) { if ( head == null ) return ; if ( head . next == null ) { maxNode = head ; return ; } populateArbit ( head . next ) ; head . arbit = maxNode ; if ( head . data > maxNode . data ) maxNode = head ; return ; } static void printNextArbitPointers ( Node node ) { System . out . println ( " Node TABSYMBOL Next ▁ Pointer TABSYMBOL Arbit ▁ Pointer " ) ; while ( node != null ) { System . out . print ( node . data + " TABSYMBOL TABSYMBOL TABSYMBOL " ) ; if ( node . next != null ) System . out . print ( node . next . data + " TABSYMBOL TABSYMBOL TABSYMBOL TABSYMBOL " ) ; else System . out . print ( " NULL " + " TABSYMBOL TABSYMBOL TABSYMBOL " ) ; if ( node . arbit != null ) System . out . print ( node . arbit . data ) ; else System . out . print ( " NULL " ) ; System . out . println ( ) ; node = node . next ; } } static Node newNode ( int data ) { Node new_node = new Node ( ) ; new_node . data = data ; new_node . next = null ; return new_node ; } public static void main ( String [ ] args ) { Node head = newNode ( 5 ) ; head . next = newNode ( 10 ) ; head . next . next = newNode ( 2 ) ; head . next . next . next = newNode ( 3 ) ; populateArbit ( head ) ; System . out . println ( " Resultant ▁ Linked ▁ List ▁ is : ▁ " ) ; printNextArbitPointers ( head ) ; } }
Check if a linked list of strings forms a palindrome | Java Program to check if a given linked list of strings form a palindrome ; Linked List node ;  ; 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 ; Check if the formed string is palindrome ; Driver program to test above function
import java . util . Scanner ; class Node { String data ; Node next ; Node ( String d ) { data = d ; next = null ; } } class LinkedList_Palindrome { Node head ; boolean isPalidromeUtil ( String str ) { int length = str . length ( ) ; for ( int i = 0 ; i < length / 2 ; i ++ ) if ( str . charAt ( i ) != str . charAt ( length - i - 1 ) ) return false ; return true ; } boolean isPalindrome ( ) { Node node = head ; String str = " " ; while ( node != null ) { str = str . concat ( node . data ) ; node = node . next ; } return isPalidromeUtil ( str ) ; } public static void main ( String [ ] args ) { LinkedList_Palindrome list = new LinkedList_Palindrome ( ) ; list . head = new Node ( " a " ) ; list . head . next = new Node ( " bc " ) ; list . head . next . next = new Node ( " d " ) ; list . head . next . next . next = new Node ( " dcb " ) ; list . head . next . next . next . next = new Node ( " a " ) ; System . out . println ( list . isPalindrome ( ) ) ; } }
Delete last occurrence of an item from linked list | Java program to demonstrate deletion of last Node in singly 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 GFG { static class Node { int data ; Node next ; } ; static void deleteLast ( Node head , int x ) { Node temp = head , ptr = null ; while ( temp != null ) { if ( temp . data == x ) ptr = temp ; temp = temp . next ; } if ( ptr != null && ptr . next == null ) { temp = head ; while ( temp . next != ptr ) temp = temp . next ; temp . next = null ; } if ( ptr != null && ptr . next != null ) { ptr . data = ptr . next . data ; temp = ptr . next ; ptr . next = ptr . next . next ; System . gc ( ) ; } } static Node newNode ( int x ) { Node node = new Node ( ) ; node . data = x ; node . next = null ; return node ; } static void display ( Node head ) { Node temp = head ; if ( head == null ) { System . out . print ( "nullNEW_LINE"); return ; } while ( temp != null ) { System . out . printf ( " % d ▁ - - > ▁ " , temp . data ) ; temp = temp . next ; } System . out . print ( "nullNEW_LINE"); } public static void main ( String [ ] args ) { Node 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 ( 4 ) ; head . next . next . next . next . next . next = newNode ( 4 ) ; System . out . print ( " Created ▁ Linked ▁ list : ▁ " ) ; display ( head ) ; deleteLast ( head , 4 ) ; System . out . print ( " List ▁ after ▁ deletion ▁ of ▁ 4 : ▁ " ) ; display ( head ) ; } }
Flatten a multi |
Node flattenList2 ( Node head ) { Node headcop = head ; Stack < Node > save = new Stack < > ( ) ; save . push ( head ) ; Node prev = null ; while ( ! save . isEmpty ( ) ) { Node temp = save . peek ( ) ; save . pop ( ) ; if ( temp . next ) save . push ( temp . next ) ; if ( temp . down ) save . push ( temp . down ) ; if ( prev != null ) prev . next = temp ; prev = temp ; } return headcop ; }
Partitioning a linked list around a given value and keeping the original order | Java program to partition a linked list around a given value . ; 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 print linked list ; Driver code ; Start with the empty list
class GfG { static class Node { int data ; Node next ; } static Node newNode ( int data ) { Node new_node = new Node ( ) ; new_node . data = data ; new_node . next = null ; return new_node ; } static Node partition ( Node head , int x ) { Node smallerHead = null , smallerLast = null ; Node greaterLast = null , greaterHead = null ; Node 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 ; } static void printList ( Node head ) { Node temp = head ; while ( temp != null ) { System . out . print ( temp . data + " ▁ " ) ; temp = temp . next ; } } public static void main ( String [ ] args ) { Node 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 ) ; int x = 3 ; head = partition ( head , x ) ; printList ( head ) ; } }
Length of longest palindrome list in a linked list using O ( 1 ) extra space | Java program to find longest palindrome sublist in a list in O ( 1 ) time . ; 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 ; Driver code ; Let us create a linked lists to test the functions Created list is a : 2 -> 4 -> 3 -> 4 -> 2 -> 15
class GfG { static class Node { int data ; Node next ; } static int countCommon ( Node a , Node b ) { int count = 0 ; for ( ; a != null && b != null ; a = a . next , b = b . next ) if ( a . data == b . data ) ++ count ; else break ; return count ; } static int maxPalindrome ( Node head ) { int result = 0 ; Node prev = null , curr = head ; while ( curr != null ) { Node 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 ; } static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . data = key ; temp . next = null ; return temp ; } public static void main ( String [ ] args ) { Node 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 ) ; System . out . println ( 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 .
import java . util . * ; class GFG { public static void main ( String [ ] args ) { ArrayList < Integer > list = new ArrayList < > ( ) ; list . add ( 1 ) ; list . add ( 2 ) ; list . add ( 3 ) ; Iterator < Integer > it = list . iterator ( ) ; while ( it . hasNext ( ) ) { System . out . print ( it . next ( ) + " ▁ " ) ; } } }
Move all occurrences of an element to end in a linked list | Java program to move all occurrences of a given key to end . ; 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 GFG { static class Node { int data ; Node next ; } static Node newNode ( int x ) { Node temp = new Node ( ) ; temp . data = x ; temp . next = null ; return temp ; } static void printList ( Node head ) { Node temp = head ; while ( temp != null ) { System . out . printf ( " % d ▁ " , temp . data ) ; temp = temp . next ; } System . out . printf ( "NEW_LINE"); } static void moveToEnd ( Node head , int key ) { Node pKey = head ; Node 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 ; } } public static void main ( String args [ ] ) { Node 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 ) ; System . out . printf ( "Before moveToEnd(), the Linked list isNEW_LINE"); printList ( head ) ; int key = 10 ; moveToEnd ( head , key ) ; System . out . printf ( " After moveToEnd ( ) , the Linked list is "); printList ( head ) ; } }
Move all occurrences of an element to end in a linked list | Java code to remove key element to end of linked list ; Node class ; 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
import java . util . * ; class Node { int data ; Node next ; public Node ( int data ) { this . data = data ; this . next = null ; } } class gfg { static Node root ; public static Node keyToEnd ( Node head , int key ) { Node tail = head ; if ( head == null ) { return null ; } while ( tail . next != null ) { tail = tail . next ; } Node last = tail ; Node current = head ; Node prev = null ; Node 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 ; } public static void display ( Node root ) { while ( root != null ) { System . out . print ( root . data + " ▁ " ) ; root = root . next ; } } public static void main ( String args [ ] ) { 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 ) ; int key = 2 ; System . out . println ( " Linked ▁ List ▁ before ▁ operations ▁ : " ) ; display ( root ) ; System . out . println ( " Linked List after operations : "); 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
class LinkedList { Node head = null ; class Node { int val ; Node next ; Node ( int v ) { val = v ; next = null ; } } public void insert ( int data ) { Node new_node = new Node ( data ) ; new_node . next = head ; head = new_node ; } public void removeAllDuplicates ( ) { Node dummy = new Node ( 0 ) ; dummy . next = head ; Node prev = dummy ; Node 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 ; } public void printList ( ) { Node trav = head ; if ( head == null ) System . out . print ( " ▁ List ▁ is ▁ empty " ) ; while ( trav != null ) { System . out . print ( trav . val + " ▁ " ) ; trav = trav . next ; } } public static void main ( String [ ] args ) { LinkedList ll = new LinkedList ( ) ; ll . insert ( 53 ) ; ll . insert ( 53 ) ; ll . insert ( 49 ) ; ll . insert ( 49 ) ; ll . insert ( 35 ) ; ll . insert ( 28 ) ; ll . insert ( 28 ) ; ll . insert ( 23 ) ; System . out . println ( " Before ▁ removal ▁ of ▁ duplicates " ) ; ll . printList ( ) ; ll . removeAllDuplicates ( ) ; System . out . println ( " After removal of duplicates "); ll . printList ( ) ; } }
Remove every k | Java 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 ; 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
class GFG { static class Node { int data ; Node next ; } static Node freeList ( Node node ) { while ( node != null ) { Node next = node . next ; node = next ; } return node ; } static Node deleteKthNode ( Node head , int k ) { if ( head == null ) return null ; if ( k == 1 ) { head = freeList ( head ) ; return null ; } Node ptr = head , prev = null ; int 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 ; } static void displayList ( Node head ) { Node temp = head ; while ( temp != null ) { System . out . print ( temp . data + " ▁ " ) ; temp = temp . next ; } } static Node newNode ( int x ) { Node temp = new Node ( ) ; temp . data = x ; temp . next = null ; return temp ; } public static void main ( String args [ ] ) { Node 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 ) ; int k = 3 ; head = deleteKthNode ( head , k ) ; displayList ( head ) ; } }
Check whether the length of given linked list is Even or Odd | Java program to check length of a given linklist ; 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
import java . io . * ; class GfG { static class Node { int data ; Node next ; } static int LinkedListLength ( Node head ) { while ( head != null && head . next != null ) { head = head . next . next ; } if ( head == null ) return 0 ; return 1 ; } static void push ( Node head , int info ) { Node node = new Node ( ) ; node . data = info ; node . next = ( head ) ; ( head ) = node ; } public static void main ( String [ ] args ) { 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 ) ; int check = LinkedListLength ( head ) ; if ( check == 0 ) { System . out . println ( " Odd " ) ; } else { System . out . println ( " Even " ) ; } } }
Find the sum of last n nodes of the given Linked List | Java implementation to find the sum of last ' n ' nodes of the 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
import java . util . * ; class GFG { static class Node { int data ; Node next ; } ; static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; return head_ref ; } static int sumOfLastN_NodesUtil ( Node head , int n ) { if ( n <= 0 ) return 0 ; Stack < Integer > st = new Stack < Integer > ( ) ; int sum = 0 ; while ( head != null ) { st . push ( head . data ) ; head = head . next ; } while ( n -- > 0 ) { sum += st . peek ( ) ; st . pop ( ) ; } return sum ; } public static void main ( String [ ] args ) { Node head = null ; head = push ( head , 12 ) ; head = push ( head , 4 ) ; head = push ( head , 8 ) ; head = push ( head , 6 ) ; head = push ( head , 10 ) ; int n = 2 ; System . out . print ( " Sum ▁ of ▁ last ▁ " + n + " ▁ nodes ▁ = ▁ " + sumOfLastN_NodesUtil ( head , n ) ) ; } }