text: Rearrange a given linked list in | Javascript 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 . ; Let create a linked list . 9 . 6 . 8 . 3 . 7
code: class Node { constructor ( ) { this . data ; this . next = null ; } } function rearrange ( head ) { if ( head == null ) return null ; let prev = head , curr = head . next ; while ( curr != null ) { if ( prev . data > curr . data ) { let t = prev . data ; prev . data = curr . data ; curr . data = t ; } if ( curr . next != null && curr . next . data > curr . data ) { let 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 ; } function push ( head , k ) { let tem = new Node ( ) ; tem . data = k ; tem . next = head ; head = tem ; return head ; } function display ( head ) { let curr = head ; while ( curr != null ) { document . write ( curr . data + " " ) ; curr = curr . next ; } } let 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 ) ;