Linked List Inserting in sorted format

Posted by user2738718 on Stack Overflow See other posts from Stack Overflow or by user2738718
Published on 2013-11-03T03:49:34Z Indexed on 2013/11/03 3:53 UTC
Read the original article Hit count: 168

Filed under:
|
package practise;
public class Node 
{
    public int data;
    public Node next;

    public Node (int data, Node next)
    {
        this.data = data;
        this.next = next;
    }

    public  int size (Node list)
    {
        int count = 0;
        while(list != null){
            list = list.next;
            count++;
        }
        return count;
    }

    public static Node insert(Node head, int value) 
    { 
        Node T;
        if (head == null || head.data <= value) 
        {
            T = new Node(value,head);
            return T;
        } 
        else  
        {
            head.next = insert(head.next, value);
            return head;
        }
    }

}

This work fine for all data values less than the first or the head. anything greater than than doesn't get added to the list.please explain in simple terms thanks.

© Stack Overflow or respective owner

Related posts about java

Related posts about linked-list