text: Rearrange a given linked list in | C # implementation ; Creating the structure for node ;  ; Function to print the list ; Function to rearrange ; We set left = null , when we reach stop condition , so no processing required after that ; Stop condition : odd case : left = right , even case : left . next = right ; Stop condition , set null to left nodes ; Even case ; Odd case ; Driver Code ; Print original list ; Modify the list ; Print modified list
code: using System ; public class Node { public int data ; public Node next ; public Node ( int key ) { data = key ; next = null ; } } class GFG { Node left = null ; void printlist ( Node head ) { while ( head != null ) { Console . Write ( head . data + " ▁ " ) ; if ( head . next != null ) { Console . Write ( " - > " ) ; } head = head . next ; } Console . WriteLine ( ) ; } void rearrange ( Node head ) { if ( head != null ) { left = head ; reorderListUtil ( left ) ; } } void reorderListUtil ( Node right ) { if ( right == null ) { return ; } reorderListUtil ( right . next ) ; if ( left == null ) { return ; } if ( left != right && left . next != right ) { Node temp = left . next ; left . next = right ; right . next = temp ; left = temp ; } else { if ( left . next == right ) { left . next . next = null ; left = null ; } else { left . next = null ; left = null ; } } } static public void Main ( ) { 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 ) ; GFG gfg = new GFG ( ) ; gfg . printlist ( head ) ; gfg . rearrange ( head ) ; gfg . printlist ( head ) ; } }