codeparrot/xlcost-text-to-code huggingface dataset:
Check linked list with a loop is palindrome or not | C ++ program to check if a linked list with 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 program to test above function ; Create a loop for testing
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * next ; } ; Node * getLoopstart ( Node * loop_node , Node * head ) { Node * ptr1 = loop_node ; Node * ptr2 = loop_node ; unsigned int 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 ; } Node * detectAndgetLoopstarting ( Node * head ) { Node * slow_p = head , * fast_p = head , * loop_start ; while ( slow_p && fast_p && fast_p -> next ) { 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 ; } bool isPalindromeUtil ( Node * head , Node * loop_start ) { Node * ptr = head ; stack < int > s ; int 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 ) { if ( ptr -> data == s . top ( ) ) s . pop ( ) ; else return false ; if ( ptr == loop_start ) count = 1 ; ptr = ptr -> next ; } return true ; } bool isPalindrome ( Node * head ) { Node * loop_start = detectAndgetLoopstarting ( head ) ; return isPalindromeUtil ( head , loop_start ) ; } Node * newNode ( int key ) { Node * temp = new Node ; temp -> data = key ; temp -> next = NULL ; return temp ; } int main ( ) { Node * 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 ; isPalindrome ( head ) ? cout << " Palindrome " : cout << " Not Palindrome " return 0 ; }
Length of longest palindrome list in a linked list using O ( 1 ) extra space | C ++ 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 program to test above functions ; Let us create a linked lists to test the functions Created list is a : 2 -> 4 -> 3 -> 4 -> 2 -> 15
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * next ; } ; int countCommon ( Node * a , Node * b ) { int count = 0 ; for ( ; a && b ; a = a -> next , b = b -> next ) if ( a -> data == b -> data ) ++ count ; else break ; return count ; } int maxPalindrome ( Node * head ) { int result = 0 ; Node * prev = NULL , * curr = head ; while ( curr ) { Node * next = curr -> next ; curr -> next = prev ; result = max ( result , 2 * countCommon ( prev , next ) + 1 ) ; result = max ( result , 2 * countCommon ( curr , next ) ) ; prev = curr ; curr = next ; } return result ; } Node * newNode ( int key ) { Node * temp = new Node ; temp -> data = key ; temp -> next = NULL ; return temp ; } int main ( ) { 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 ) ; cout << maxPalindrome ( head ) << endl ; return 0 ; }
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 .
#include <bits/stdc++.h> NEW_LINE using namespace std ; int main ( ) { vector < int > list ; list . push_back ( 1 ) ; list . push_back ( 2 ) ; list . push_back ( 3 ) ; for ( vector < int > :: iterator it = list . begin ( ) ; it != list . end ( ) ; ++ it ) cout << * it << " ▁ " ; return 0 ; }
Move all occurrences of an element to end in a linked list | C ++ 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
#include <bits/stdc++.h> NEW_LINE struct Node { int data ; struct Node * next ; } ; struct Node * newNode ( int x ) { Node * temp = new Node ; temp -> data = x ; temp -> next = NULL ; } void printList ( Node * head ) { struct Node * temp = head ; while ( temp != NULL ) { printf ( " % d ▁ " , temp -> data ) ; temp = temp -> next ; } printf ( " STRNEWLINE " ) ; } void moveToEnd ( Node * head , int key ) { struct Node * pKey = head ; struct 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 ; } } int main ( ) { 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 ) ; printf ( " Before ▁ moveToEnd ( ) , ▁ the ▁ Linked ▁ list ▁ is STRNEWLINE " ) ; printList ( head ) ; int key = 10 ; moveToEnd ( head , key ) ; printf ( " After moveToEnd ( ) , the Linked list is " printList ( head ) ; return 0 ; }
Move all occurrences of an element to end in a linked list | C ++ code to remove key element to end of linked list ; A Linked list Node ; Function to remove key to end ; Node to keep pointing to tail ; Node to point to last of linked list ; Node prev2 to point to previous when head . data != key ; loop to perform operations to remove key to end ; Function to display linked list ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * next ; } ; struct Node * newNode ( int x ) { Node * temp = new Node ; temp -> data = x ; temp -> next = NULL ; } 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 ; } void printList ( Node * head ) { struct Node * temp = head ; while ( temp != NULL ) { printf ( " % d ▁ " , temp -> data ) ; temp = temp -> next ; } printf ( " STRNEWLINE " ) ; } int main ( ) { Node * root = newNode ( 5 ) ; root -> next = newNode ( 2 ) ; root -> next -> next = newNode ( 2 ) ; root -> next -> next -> next = newNode ( 7 ) ; root -> next -> next -> next -> next = newNode ( 2 ) ; root -> next -> next -> next -> next -> next = newNode ( 2 ) ; root -> next -> next -> next -> next -> next -> next = newNode ( 2 ) ; int key = 2 ; cout << " Linked ▁ List ▁ before ▁ operations ▁ : " ; printList ( root ) ; cout << " Linked List after operations : " root = keyToEnd ( root , key ) ; printList ( root ) ; return 0 ; }
Remove every k | C ++ 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 program to test count function ; Start with the empty list
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * next ; } ; void freeList ( Node * node ) { while ( node != NULL ) { Node * next = node -> next ; delete ( node ) ; node = next ; } } Node * deleteKthNode ( struct Node * head , int k ) { if ( head == NULL ) return NULL ; if ( k == 1 ) { freeList ( head ) ; return NULL ; } struct Node * ptr = head , * prev = NULL ; int count = 0 ; while ( ptr != NULL ) { count ++ ; if ( k == count ) { delete ( prev -> next ) ; prev -> next = ptr -> next ; count = 0 ; } if ( count != 0 ) prev = ptr ; ptr = prev -> next ; } return head ; } void displayList ( struct Node * head ) { struct Node * temp = head ; while ( temp != NULL ) { cout << temp -> data << " ▁ " ; temp = temp -> next ; } } struct Node * newNode ( int x ) { Node * temp = new Node ; temp -> data = x ; temp -> next = NULL ; return temp ; } int main ( ) { struct 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 ) ; return 0 ; }
Check whether the length of given linked list is Even or Odd | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; class Node { public : int data ; Node * next ; } ; int LinkedListLength ( Node * head ) { while ( head && head -> next ) { head = head -> next -> next ; } if ( ! head ) return 0 ; return 1 ; } void push ( Node * * head , int info ) { Node * node = new Node ( ) ; node -> data = info ; node -> next = ( * head ) ; ( * head ) = node ; } int main ( void ) { 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 ) { cout << " Even STRNEWLINE " ; } else { cout << " Odd STRNEWLINE " ; } return 0 ; }
Find the sum of last n nodes of the given Linked List | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * next ; } ; void push ( struct Node * * head_ref , int new_data ) { struct Node * new_node = new Node ; new_node -> data = new_data ; new_node -> next = ( * head_ref ) ; ( * head_ref ) = new_node ; } int sumOfLastN_NodesUtil ( struct Node * head , int n ) { if ( n <= 0 ) return 0 ; stack < int > st ; int sum = 0 ; while ( head != NULL ) { st . push ( head -> data ) ; head = head -> next ; } while ( n -- ) { sum += st . top ( ) ; st . pop ( ) ; } return sum ; } int main ( ) { struct Node * head = NULL ; push ( & head , 12 ) ; push ( & head , 4 ) ; push ( & head , 8 ) ; push ( & head , 6 ) ; push ( & head , 10 ) ; int n = 2 ; cout << " Sum ▁ of ▁ last ▁ " << n << " ▁ nodes ▁ = ▁ " << sumOfLastN_NodesUtil ( head , n ) ; return 0 ; }
Find the sum of last n nodes of the given Linked List | C ++ 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 ; reverse the linked list ; traverse the 1 st ' n ' nodes of the reversed linked list and add them ; accumulate node ' s ▁ data ▁ to ▁ ' sum ' ; move to next node ; reverse back the linked list ; required sum ; Driver program to test above ; create linked list 10 -> 6 -> 8 -> 4 -> 12
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * next ; } ; void push ( struct Node * * head_ref , int new_data ) { struct Node * new_node = new Node ; new_node -> data = new_data ; new_node -> next = ( * head_ref ) ; ( * head_ref ) = new_node ; } void reverseList ( struct Node * * head_ref ) { struct Node * current , * prev , * next ; current = * head_ref ; prev = NULL ; while ( current != NULL ) { next = current -> next ; current -> next = prev ; prev = current ; current = next ; } * head_ref = prev ; } int sumOfLastN_NodesUtil ( struct Node * head , int n ) { if ( n <= 0 ) return 0 ; reverseList ( & head ) ; int sum = 0 ; struct Node * current = head ; while ( current != NULL && n -- ) { sum += current -> data ; current = current -> next ; } reverseList ( & head ) ; return sum ; } int main ( ) { struct Node * head = NULL ; push ( & head , 12 ) ; push ( & head , 4 ) ; push ( & head , 8 ) ; push ( & head , 6 ) ; push ( & head , 10 ) ; int n = 2 ; cout << " Sum ▁ of ▁ last ▁ " << n << " ▁ nodes ▁ = ▁ " << sumOfLastN_NodesUtil ( head , n ) ; return 0 ; }
Find the sum of last n nodes of the given Linked List | C ++ 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 ; calculate the length of the linked list ; count of first ( len - n ) nodes ; just traverse the 1 st ' c ' nodes ; move to next node ; now traverse the last ' n ' nodes and add them ; accumulate node 's data to sum ; move to next node ; required sum ; Driver program to test above ; create linked list 10 -> 6 -> 8 -> 4 -> 12
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * next ; } ; void push ( struct Node * * head_ref , int new_data ) { struct Node * new_node = new Node ; new_node -> data = new_data ; new_node -> next = ( * head_ref ) ; ( * head_ref ) = new_node ; } int sumOfLastN_NodesUtil ( struct Node * head , int n ) { if ( n <= 0 ) return 0 ; int sum = 0 , len = 0 ; struct Node * temp = head ; while ( temp != NULL ) { len ++ ; temp = temp -> next ; } int c = len - n ; temp = head ; while ( temp != NULL && c -- ) temp = temp -> next ; while ( temp != NULL ) { sum += temp -> data ; temp = temp -> next ; } return sum ; } int main ( ) { struct Node * head = NULL ; push ( & head , 12 ) ; push ( & head , 4 ) ; push ( & head , 8 ) ; push ( & head , 6 ) ; push ( & head , 10 ) ; int n = 2 ; cout << " Sum ▁ of ▁ last ▁ " << n << " ▁ nodes ▁ = ▁ " << sumOfLastN_NodesUtil ( head , n ) ; return 0 ; }
Merge two sorted linked lists |  ; point to the last result pointer ; tricky : advance to point to the next " . next " field
Node * SortedMerge ( Node * a , Node * b ) { Node * result = NULL ; Node * * 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 ) ; }
In | C ++ Program to merge two sorted linked lists without using any extra space and without changing links of first list ; Structure for a linked list node ; Given a reference ( pointer to pointer ) to the head of a list and an int , push a new node on the front of the list . ; allocate node ; put in the data ; link the old list off the new node ; move the head to point to the new node ; Function to merge two sorted linked lists LL1 and LL2 without using any extra space . ; run till either one of a or b runs out ; for each element of LL1 , compare it with first element of LL2 . ; swap the two elements involved if LL1 has a greater element ; To keep LL2 sorted , place first element of LL2 at its correct place ; find mismatch by traversing the second linked list once ; correct the pointers ; move LL1 pointer to next element ; Code to print the linked link ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * next ; } ; void push ( struct Node * * head_ref , int new_data ) { struct Node * new_node = ( struct Node * ) malloc ( sizeof ( struct Node ) ) ; new_node -> data = new_data ; new_node -> next = ( * head_ref ) ; ( * head_ref ) = new_node ; } void mergeLists ( struct Node * a , struct Node * & b ) { while ( a && b ) { if ( a -> data > b -> data ) { swap ( a -> data , b -> data ) ; struct Node * temp = b ; if ( b -> next && b -> data > b -> next -> data ) { b = b -> next ; struct Node * ptr = b , * prev = NULL ; while ( ptr && ptr -> data < temp -> data ) { prev = ptr ; ptr = ptr -> next ; } prev -> next = temp ; temp -> next = ptr ; } } a = a -> next ; } } void printList ( struct Node * head ) { while ( head ) { cout << head -> data << " - > " ; head = head -> next ; } cout << " NULL " << endl ; } int main ( ) { struct Node * a = NULL ; push ( & a , 10 ) ; push ( & a , 8 ) ; push ( & a , 7 ) ; push ( & a , 4 ) ; push ( & a , 2 ) ; struct Node * b = NULL ; push ( & b , 12 ) ; push ( & b , 3 ) ; push ( & b , 1 ) ; mergeLists ( a , b ) ; cout << " First ▁ List : ▁ " ; printList ( a ) ; cout << " Second ▁ List : ▁ " ; printList ( b ) ; return 0 ; }
Delete middle of linked list | C ++ program to delete middle of a linked list ; Link list Node ; count of nodes ; Deletes middle node and returns head of the modified list ; Base cases ; Find the count of nodes ; Find the middle node ; Delete the middle node ; Delete the middle node ; A utility function to print a given linked list ; Utility function to create a new node . ; Driver program to test above function ; Start with the empty list
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * next ; } ; int countOfNodes ( struct Node * head ) { int count = 0 ; while ( head != NULL ) { head = head -> next ; count ++ ; } return count ; } struct Node * deleteMid ( struct Node * head ) { if ( head == NULL ) return NULL ; if ( head -> next == NULL ) { delete head ; return NULL ; } struct Node * copyHead = head ; int count = countOfNodes ( head ) ; int mid = count / 2 ; while ( mid -- > 1 ) { head = head -> next ; } head -> next = head -> next -> next ; return copyHead ; } void printList ( struct Node * ptr ) { while ( ptr != NULL ) { cout << ptr -> data << " - > " ; ptr = ptr -> next ; } cout << " NULL STRNEWLINE " ; } Node * newNode ( int data ) { struct Node * temp = new Node ; temp -> data = data ; temp -> next = NULL ; return temp ; } int main ( ) { struct Node * head = newNode ( 1 ) ; head -> next = newNode ( 2 ) ; head -> next -> next = newNode ( 3 ) ; head -> next -> next -> next = newNode ( 4 ) ; cout << " Given ▁ Linked ▁ List STRNEWLINE " ; printList ( head ) ; head = deleteMid ( head ) ; cout << " Linked ▁ List ▁ after ▁ deletion ▁ of ▁ middle STRNEWLINE " ; printList ( head ) ; return 0 ; }