Insert node to a sorted list
Insert node to a sorted list
// Insert node to a sorted list
    public static SNode insert(SNode node, int n){
        SNode head = node;
        SNode curr = node; 
        SNode prev = null; 
        while(curr != null && n > curr.data){
            prev = curr;
            curr = curr.next;
        }

        if(curr != null){
            if(prev != null){
                // 3, [2]<-[4]  >  [2]<-{3}<-[4]
                prev.next = new SNode(n);
                prev.next.next = curr;
            }else{
                // {1} [2] > {1}<-[2]
                head = new SNode(n);
                head.next = curr;
            }
        }else{
            // [2] {3}  > [2]<-{3}
            if(prev != null){
                prev.next = new SNode(n);
            }else{
                // {3}
                head = new SNode(n);
            }
        }
        return head;
    }