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: using System ; class GFG { class Node { public int data ; public Node next ; } static Node rearrange ( Node head ) { if ( head == null ) return null ; Node prev = head , curr = head . next ; while ( curr != null ) { if ( prev . data > curr . data ) { int t = prev . data ; prev . data = curr . data ; curr . data = t ; } if ( curr . next != null && curr . next . data > curr . data ) { int t = curr . next . data ; curr . next . data = curr . data ; curr . data = t ; } prev = curr . next ; if ( curr . next == null ) break ; curr = curr . next . next ; } return head ; } static Node push ( Node head , int k ) { Node tem = new Node ( ) ; tem . data = k ; tem . next = head ; head = tem ; return head ; } static void display ( Node head ) { Node curr = head ; while ( curr != null ) { Console . Write ( curr . data + " ▁ " ) ; curr = curr . next ; } } public static void Main ( string [ ] args ) { Node head = null ; head = push ( head , 7 ) ; head = push ( head , 3 ) ; head = push ( head , 8 ) ; head = push ( head , 6 ) ; head = push ( head , 9 ) ; head = rearrange ( head ) ; display ( head ) ; } }