text: Rearrange a given linked list in | C ++ code to rearrange linked list in place ; function for rearranging a linked list with high and low value . ; Base case . ; two pointer variable . ; swap function for swapping data . ; swap function for swapping data . ; function to insert a node in the linked list at the beginning . ; function to display node of linked list . ; driver code ; let create a linked list . 9 -> 6 -> 8 -> 3 -> 7
code: #include <bits/stdc++.h> NEW_LINE using namespace std ; struct node { int data ; struct node * next ; } ; typedef struct node Node ; void rearrange ( Node * head ) { if ( head == NULL ) return ; Node * prev = head , * curr = head -> next ; while ( curr ) { if ( prev -> data > curr -> data ) swap ( prev -> data , curr -> data ) ; if ( curr -> next && curr -> next -> data > curr -> data ) swap ( curr -> next -> data , curr -> data ) ; prev = curr -> next ; if ( ! curr -> next ) break ; curr = curr -> next -> next ; } } void push ( Node * * head , int k ) { Node * tem = ( Node * ) malloc ( sizeof ( Node ) ) ; tem -> data = k ; tem -> next = * head ; * head = tem ; } void display ( Node * head ) { Node * curr = head ; while ( curr != NULL ) { printf ( " % d ▁ " , curr -> data ) ; curr = curr -> next ; } } int main ( ) { Node * head = NULL ; push ( & head , 7 ) ; push ( & head , 3 ) ; push ( & head , 8 ) ; push ( & head , 6 ) ; push ( & head , 9 ) ; rearrange ( head ) ; display ( head ) ; return 0 ; }