codeparrot/xlcost-text-to-code huggingface dataset:
Count triplets in a sorted doubly linked list whose sum is equal to a given value x | Java implementation to count triplets in a sorted doubly linked list whose sum is equal to a given value ' x ' ; structure of node of doubly linked list ; function to count pairs whose sum equal to given ' value ' ; The loop terminates when either of two pointers become null , or they cross each other ( second . next == first ) , or they become same ( first == second ) ; pair found ; increment count ; move first in forward direction ; move second in backward direction ; if sum is greater than ' value ' move second in backward direction ; else move first in forward direction ; required count of pairs ; function to count triplets in a sorted doubly linked list whose sum is equal to a given value ' x ' ; if list is empty ; get pointer to the last node of the doubly linked list ; traversing the doubly linked list ; for each current node ; count pairs with sum ( x - current . data ) in the range first to last and add it to the ' count ' of triplets ; required count of triplets ; A utility function to insert a new node at the beginning of doubly linked list ; allocate node ; put in the data ; Driver program to test above ; start with an empty doubly linked list ; insert values in sorted order
import java . util . * ; class GFG { static class Node { int data ; Node next , prev ; } ; static int countPairs ( Node first , Node second , int value ) { int count = 0 ; while ( first != null && second != null && first != second && second . next != first ) { if ( ( first . data + second . data ) == value ) { count ++ ; first = first . next ; second = second . prev ; } else if ( ( first . data + second . data ) > value ) second = second . prev ; else first = first . next ; } return count ; } static int countTriplets ( Node head , int x ) { if ( head == null ) return 0 ; Node current , first , last ; int count = 0 ; last = head ; while ( last . next != null ) last = last . next ; for ( current = head ; current != null ; current = current . next ) { first = current . next ; count += countPairs ( first , last , x - current . data ) ; } return count ; } static Node insert ( Node head , int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . next = temp . prev = null ; if ( ( head ) == null ) ( head ) = temp ; else { temp . next = head ; ( head ) . prev = temp ; ( head ) = temp ; } return head ; } public static void main ( String [ ] args ) { Node head = null ; head = insert ( head , 9 ) ; head = insert ( head , 8 ) ; head = insert ( head , 6 ) ; head = insert ( head , 5 ) ; head = insert ( head , 4 ) ; head = insert ( head , 2 ) ; head = insert ( head , 1 ) ; int x = 17 ; System . out . print ( " Count ▁ = ▁ " + countTriplets ( head , x ) ) ; } }
Remove duplicates from an unsorted doubly linked list | Java implementation to remove duplicates from an unsorted doubly linked list ; a node of the doubly linked list ; Function to delete a node in a Doubly Linked List . head_ref - . pointer to head node pointer . del - . pointer to node to be deleted . ; base case ; If node to be deleted is head node ; Change next only if node to be deleted is NOT the last node ; Change prev only if node to be deleted is NOT the first node ; function to remove duplicates from an unsorted doubly linked list ; if DLL is empty or if it contains only a single node ; pick elements one by one ; Compare the picked element with the rest of the elements ; if duplicate , then delete it ; store pointer to the node next to ' ptr2' ; delete node pointed to by ' ptr2' ; update ' ptr2' ; else simply move to the next node ; Function to insert a node at the beginning of the Doubly Linked List ; allocate node ; put in the data ; since we are adding at the beginning , prev is always null ; link the old list off the new node ; change prev of head node to new node ; move the head to point to the new node ; Function to print nodes in a given doubly linked list ; if list is empty ; Driver Code ; Create the doubly linked list : 8 < .4 < .4 < .6 < .4 < .8 < .4 < .10 < .12 < .12 ; remove duplicate nodes
class GFG { static class Node { int data ; Node next ; Node prev ; } static Node deleteNode ( Node head_ref , Node del ) { if ( head_ref == null del == null ) return head_ref ; if ( head_ref == del ) head_ref = del . next ; if ( del . next != null ) del . next . prev = del . prev ; if ( del . prev != null ) del . prev . next = del . next ; return head_ref ; } static Node removeDuplicates ( Node head_ref ) { if ( ( head_ref ) == null || ( head_ref ) . next == null ) return head_ref ; ; Node ptr1 , ptr2 ; for ( ptr1 = head_ref ; ptr1 != null ; ptr1 = ptr1 . next ) { ptr2 = ptr1 . next ; while ( ptr2 != null ) { if ( ptr1 . data == ptr2 . data ) { Node next = ptr2 . next ; head_ref = deleteNode ( head_ref , ptr2 ) ; ptr2 = next ; } else ptr2 = ptr2 . next ; } } return head_ref ; } static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . prev = null ; new_node . next = ( head_ref ) ; if ( ( head_ref ) != null ) ( head_ref ) . prev = new_node ; ( head_ref ) = new_node ; return head_ref ; } static void printList ( Node head ) { if ( head == null ) System . out . print ( " Doubly ▁ Linked ▁ list ▁ empty " ) ; while ( head != null ) { System . out . print ( head . data + " ▁ " ) ; head = head . next ; } } public static void main ( String args [ ] ) { Node head = null ; head = push ( head , 12 ) ; head = push ( head , 12 ) ; head = push ( head , 10 ) ; head = push ( head , 4 ) ; head = push ( head , 8 ) ; head = push ( head , 4 ) ; head = push ( head , 6 ) ; head = push ( head , 4 ) ; head = push ( head , 4 ) ; head = push ( head , 8 ) ; System . out . print ( "Original Doubly linked list:NEW_LINE"); printList ( head ) ; head = removeDuplicates ( head ) ; System . out . print ( " Doubly linked list after " ▁ + ▁ " removing duplicates : "); printList ( head ) ; } }
Sorted insert in a doubly linked list with head and tail pointers | Java program to insetail nodes in doubly linked list such that list remains in ascending order on printing from left to right ; A linked list node ; Function to insetail new node ; If first node to be insetailed in doubly linked list ; If node to be insetailed has value less than first node ; If node to be insetailed has value more than last node ; Find the node before which we need to insert p . ; Insert new node before temp ; Function to print nodes in from left to right ; Driver code
import java . io . * ; import java . util . * ; class Node { int info ; Node prev , next ; } class GFG { static Node head , tail ; static void nodeInsetail ( int key ) { Node p = new Node ( ) ; p . info = key ; p . next = null ; if ( head == null ) { head = p ; tail = p ; head . prev = null ; return ; } if ( p . info < head . info ) { p . prev = null ; head . prev = p ; p . next = head ; head = p ; return ; } if ( p . info > tail . info ) { p . prev = tail ; tail . next = p ; tail = p ; return ; } Node temp = head . next ; while ( temp . info < p . info ) temp = temp . next ; ( temp . prev ) . next = p ; p . prev = temp . prev ; temp . prev = p ; p . next = temp ; } static void printList ( Node temp ) { while ( temp != null ) { System . out . print ( temp . info + " ▁ " ) ; temp = temp . next ; } } public static void main ( String args [ ] ) { head = tail = null ; nodeInsetail ( 30 ) ; nodeInsetail ( 50 ) ; nodeInsetail ( 90 ) ; nodeInsetail ( 10 ) ; nodeInsetail ( 40 ) ; nodeInsetail ( 110 ) ; nodeInsetail ( 60 ) ; nodeInsetail ( 95 ) ; nodeInsetail ( 23 ) ; System . out . println ( " Doubly ▁ linked ▁ list ▁ on ▁ printing ▁ from ▁ left ▁ to ▁ right " ) ; printList ( head ) ; } }
Doubly Circular Linked List | Set 1 ( Introduction and Insertion ) | Function to insert at the end ; If the list is empty , create a single node circular and doubly list ; If list is not empty Find last node ; Create Node dynamically ; Start is going to be next of new_node ; Make new node previous of start ; Make last preivous of new node ; Make new node next of old last
static void insertEnd ( int value ) { if ( start == null ) { Node new_node = new Node ( ) ; new_node . data = value ; new_node . next = new_node . prev = new_node ; start = new_node ; return ; } Node last = ( start ) . prev ; Node new_node = new Node ( ) ; new_node . data = value ; new_node . next = start ; ( start ) . prev = new_node ; new_node . prev = last ; last . next = new_node ; }
Doubly Circular Linked List | Set 1 ( Introduction and Insertion ) | Java program to illustrate inserting a Node in a Cicular Doubly Linked list in begging , end and middle ; Structure of a Node ; Function to insert at the end ; If the list is empty , create a single node circular and doubly list ; Find last node ; Create Node dynamically ; Start is going to be next of new_node ; Make new node previous of start ; Make last preivous of new node ; Make new node next of old last ; Function to insert Node at the beginning of the List , ; Pointer points to last Node ; Inserting the data ; setting up previous and next of new node ; Update next and previous pointers of start and last . ; Update start pointer ; Function to insert node with value as value1 . The new node is inserted after the node with with value2 ; Inserting the data ; Find node having value2 and next node of it ; insert new_node between temp and next . ; Driver code ; Start with the empty list ; Insert 5. So linked list becomes 5. null ; Insert 4 at the beginning . So linked list becomes 4.5 ; Insert 7 at the end . So linked list becomes 4.5 . 7 ; Insert 8 at the end . So linked list becomes 4.5 . 7.8 ; Insert 6 , after 5. So linked list becomes 4.5 . 6.7 . 8
import java . util . * ; class GFG { static Node start ; static class Node { int data ; Node next ; Node prev ; } ; static void insertEnd ( int value ) { if ( start == null ) { Node new_node = new Node ( ) ; new_node . data = value ; new_node . next = new_node . prev = new_node ; start = new_node ; return ; } Node last = ( start ) . prev ; Node new_node = new Node ( ) ; new_node . data = value ; new_node . next = start ; ( start ) . prev = new_node ; new_node . prev = last ; last . next = new_node ; } static void insertBegin ( int value ) { Node last = ( start ) . prev ; Node new_node = new Node ( ) ; new_node . data = value ; new_node . next = start ; new_node . prev = last ; last . next = ( start ) . prev = new_node ; start = new_node ; } static void insertAfter ( int value1 , int value2 ) { Node new_node = new Node ( ) ; new_node . data = value1 ; Node temp = start ; while ( temp . data != value2 ) temp = temp . next ; Node next = temp . next ; temp . next = new_node ; new_node . prev = temp ; new_node . next = next ; next . prev = new_node ; } static void display ( ) { Node temp = start ; System . out . printf ( " Traversal in forward direction "); while ( temp . next != start ) { System . out . printf ( " % d ▁ " , temp . data ) ; temp = temp . next ; } System . out . printf ( " % d ▁ " , temp . data ) ; System . out . printf ( " Traversal in reverse direction "); Node last = start . prev ; temp = last ; while ( temp . prev != last ) { System . out . printf ( " % d ▁ " , temp . data ) ; temp = temp . prev ; } System . out . printf ( " % d ▁ " , temp . data ) ; } public static void main ( String [ ] args ) { Node start = null ; insertEnd ( 5 ) ; insertBegin ( 4 ) ; insertEnd ( 7 ) ; insertEnd ( 8 ) ; insertAfter ( 6 , 5 ) ; System . out . printf ( " Created ▁ circular ▁ doubly ▁ linked ▁ list ▁ is : ▁ " ) ; display ( ) ; } }
Boundary Traversal of binary tree | A binary tree node has data , pointer to left child and a pointer to right child ; A simple function to print leaf nodes of a binary tree ; Print it if it is a leaf node ; A function to print all left boundary nodes , except a leaf node . Print the nodes in TOP DOWN manner ; to ensure top down order , print the node before calling itself for left subtree ; do nothing if it is a leaf node , this way we avoid duplicates in output ; A function to print all right boundary nodes , except a leaf node Print the nodes in BOTTOM UP manner ; to ensure bottom up order , first call for right subtree , then print this node ; do nothing if it is a leaf node , this way we avoid duplicates in output ; A function to do boundary traversal of a given binary tree ; Print the left boundary in top - down manner . ; Print all leaf nodes ; Print the right boundary in bottom - up manner ; Driver program to test above functions
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; void printLeaves ( Node node ) { if ( node == null ) return ; printLeaves ( node . left ) ; if ( node . left == null && node . right == null ) System . out . print ( node . data + " ▁ " ) ; printLeaves ( node . right ) ; } void printBoundaryLeft ( Node node ) { if ( node == null ) return ; if ( node . left != null ) { System . out . print ( node . data + " ▁ " ) ; printBoundaryLeft ( node . left ) ; } else if ( node . right != null ) { System . out . print ( node . data + " ▁ " ) ; printBoundaryLeft ( node . right ) ; } } void printBoundaryRight ( Node node ) { if ( node == null ) return ; if ( node . right != null ) { printBoundaryRight ( node . right ) ; System . out . print ( node . data + " ▁ " ) ; } else if ( node . left != null ) { printBoundaryRight ( node . left ) ; System . out . print ( node . data + " ▁ " ) ; } } void printBoundary ( Node node ) { if ( node == null ) return ; System . out . print ( node . data + " ▁ " ) ; printBoundaryLeft ( node . left ) ; printLeaves ( node . left ) ; printLeaves ( node . right ) ; printBoundaryRight ( node . right ) ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 20 ) ; tree . root . left = new Node ( 8 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 12 ) ; tree . root . left . right . left = new Node ( 10 ) ; tree . root . left . right . right = new Node ( 14 ) ; tree . root . right = new Node ( 22 ) ; tree . root . right . right = new Node ( 25 ) ; tree . printBoundary ( tree . root ) ; } }
An interesting method to print reverse of a linked list | Java program to print reverse of list ; Represents node of a linkedlist ; Function to reverse the linked list ; For each node , print proper number of spaces before printing it ; use of carriage return to move back and print . ; Function to push a node ; Function to print linked list and find its length ; i for finding length of list ; Driver code ; Start with the empty list ; list nodes are as 6 5 4 3 2 1 ; printlist print the list and return the size of list ; print reverse list with help of carriage return function
import java . io . * ; import java . util . * ; class Node { int data ; Node next ; Node ( int val ) { data = val ; next = null ; } } public class GFG { static void printReverse ( Node head , int n ) { int j = 0 ; Node current = head ; while ( current != null ) { for ( int i = 0 ; i < 2 * ( n - j ) ; i ++ ) System . out . print ( " ▁ " ) ; System . out . print ( " \r " + current . data ) ; current = current . next ; j ++ ; } } static Node push ( Node head , int data ) { Node new_node = new Node ( data ) ; new_node . next = head ; head = new_node ; return head ; } static int printList ( Node head ) { int i = 0 ; Node temp = head ; while ( temp != null ) { System . out . print ( temp . data + " ▁ " ) ; temp = temp . next ; i ++ ; } return i ; } public static void main ( String args [ ] ) { Node head = null ; head = push ( head , 1 ) ; head = push ( head , 2 ) ; head = push ( head , 3 ) ; head = push ( head , 4 ) ; head = push ( head , 5 ) ; head = push ( head , 6 ) ; System . out . println ( " Given ▁ linked ▁ list : ▁ " ) ; int n = printList ( head ) ; System . out . println ( " Reversed ▁ Linked ▁ list : ▁ " ) ; printReverse ( head , n ) ; System . out . println ( ) ; } }
Practice questions for Linked List and Recursion |
static class Node { int data ; Node next ; } ;
Practice questions for Linked List and Recursion |
static void fun1 ( Node head ) { if ( head == null ) { return ; } fun1 ( head . next ) ; System . out . print ( head . data + " ▁ " ) ; }
Practice questions for Linked List and Recursion |
static void fun2 ( Node head ) { if ( head == null ) { return ; } System . out . print ( head . data + " ▁ " ) ; if ( head . next != null ) { fun2 ( head . next . next ) ; } System . out . print ( head . data + " ▁ " ) ; }
Practice questions for Linked List and Recursion | Java code implementation for above approach ; A linked list node ; Prints a linked list in reverse manner ; prints alternate nodes of a Linked List , first from head to end , and then from end to head . ; 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 ; Driver code ; Start with the empty list ; Using push ( ) to conbelow list 1 -> 2 -> 3 -> 4 -> 5
class GFG { static class Node { int data ; Node next ; } ; static void fun1 ( Node head ) { if ( head == null ) { return ; } fun1 ( head . next ) ; System . out . print ( head . data + " ▁ " ) ; } static void fun2 ( Node start ) { if ( start == null ) { return ; } System . out . print ( start . data + " ▁ " ) ; if ( start . next != null ) { fun2 ( start . next . next ) ; } System . out . print ( start . data + " ▁ " ) ; } 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 ; } public static void main ( String [ ] args ) { Node head = null ; head = push ( head , 5 ) ; head = push ( head , 4 ) ; head = push ( head , 3 ) ; head = push ( head , 2 ) ; head = push ( head , 1 ) ; System . out . print ( " Output ▁ of ▁ fun1 ( ) ▁ for ▁ " + "list 1->2->3->4->5 NEW_LINE"); fun1 ( head ) ; System . out . print ( " Output of fun2 ( ) for " ▁ + ▁ " list 1 -> 2 -> 3 -> 4 -> 5 "); fun2 ( head ) ; } }
Density of Binary Tree in One Traversal | A binary tree node ; Class to implement pass by reference of size ; Function to compute height and size of a binary tree ; compute height of each subtree ; increase size by 1 ; return larger of the two ; function to calculate density of a binary tree ; Finds height and size ; Driver code to test above methods
class Node { int data ; Node left , right ; public Node ( int data ) { this . data = data ; left = right = null ; } } class Size { int size = 0 ; } class BinaryTree { Node root ; int heighAndSize ( Node node , Size size ) { if ( node == null ) return 0 ; int l = heighAndSize ( node . left , size ) ; int r = heighAndSize ( node . right , size ) ; size . size ++ ; return ( l > r ) ? l + 1 : r + 1 ; } float density ( Node root ) { Size size = new Size ( ) ; if ( root == null ) return 0 ; int _height = heighAndSize ( root , size ) ; return ( float ) size . size / _height ; } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; System . out . println ( " Density ▁ of ▁ given ▁ Binary ▁ Tree ▁ is ▁ : ▁ " + tree . density ( tree . root ) ) ; } }
Squareroot ( n ) | Java program to find sqrt ( n ) 'th node  of a linked list  ; Linked list node ; Function to get the sqrt ( n ) th node of a linked list ; Traverse the list ; check if j = sqrt ( i ) ; for first node ; increment j if j = sqrt ( i ) ; return node 's data  ; function to add a new node at the beginning 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 ; Driver code ; Start with the empty list
class GfG { static class Node { int data ; Node next ; } static Node head = null ; static int printsqrtn ( Node head ) { Node sqrtn = null ; int i = 1 , j = 1 ; while ( head != null ) { if ( i == j * j ) { if ( sqrtn == null ) sqrtn = head ; else sqrtn = sqrtn . next ; j ++ ; } i ++ ; head = head . next ; } return sqrtn . data ; } static void print ( Node head ) { while ( head != null ) { System . out . print ( head . data + " ▁ " ) ; head = head . next ; } System . out . println ( ) ; } static void push ( int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head ; head = new_node ; } public static void main ( String [ ] args ) { push ( 40 ) ; push ( 30 ) ; push ( 20 ) ; push ( 10 ) ; System . out . print ( " Given ▁ linked ▁ list ▁ is : " ) ; print ( head ) ; System . out . print ( " sqrt ( n ) th ▁ node ▁ is ▁ " + printsqrtn ( head ) ) ; } }
Find the fractional ( or n / k | Java program to find fractional node in a linked list ; Linked list node ; Constructor ; Function to find fractional node in the linked list ; Corner cases ; Traverse the given list ; For every k nodes , we move fractionalNode one step ahead . ; First time we see a multiple of k ; A utility function to print a linked list ; Driver program to test above function
public class FractionalNodell { static class Node { int data ; Node next ; Node ( int data ) { this . data = data ; } } static Node fractionalNodes ( Node head , int k ) { if ( k <= 0 head == null ) return null ; Node fractionalNode = null ; int i = 0 ; for ( Node temp = head ; temp != null ; temp = temp . next ) { if ( i % k == 0 ) { if ( fractionalNode == null ) fractionalNode = head ; else fractionalNode = fractionalNode . next ; } i ++ ; } return fractionalNode ; } static void printList ( Node node ) { while ( node != null ) { System . out . print ( node . data + " ▁ " ) ; node = node . next ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { Node head = new Node ( 1 ) ; head . next = new Node ( 2 ) ; head . next . next = new Node ( 3 ) ; head . next . next . next = new Node ( 4 ) ; head . next . next . next . next = new Node ( 5 ) ; int k = 2 ; System . out . print ( " List ▁ is ▁ " ) ; printList ( head ) ; Node answer = fractionalNodes ( head , k ) ; System . out . println ( " Fractional ▁ node ▁ is ▁ " + answer . data ) ; } }
Find modular node in a linked list | A Java program to find modular node in a linked list ; A Linkedlist node ; Function to find modular node in the linked list ; Corner cases ; Traverse the given list ; Driver code to test above function
public class GFG { static class Node { int data ; Node next ; Node ( int data ) { this . data = data ; } } static Node modularNode ( Node head , int k ) { if ( k <= 0 head == null ) return null ; int i = 1 ; Node modularNode = null ; for ( Node temp = head ; temp != null ; temp = temp . next ) { if ( i % k == 0 ) modularNode = temp ; i ++ ; } return modularNode ; } public static void main ( String [ ] args ) { Node head = new Node ( 1 ) ; head . next = new Node ( 2 ) ; head . next . next = new Node ( 3 ) ; head . next . next . next = new Node ( 4 ) ; head . next . next . next . next = new Node ( 5 ) ; int k = 2 ; Node answer = modularNode ( head , k ) ; System . out . print ( " Modular ▁ node ▁ is ▁ " ) ; if ( answer != null ) System . out . println ( answer . data ) ; else System . out . println ( " null " ) ; } }