Linked List. Insert integers in order

Posted by user69514 on Stack Overflow See other posts from Stack Overflow or by user69514
Published on 2010-05-02T20:42:15Z Indexed on 2010/05/02 20:47 UTC
Read the original article Hit count: 157

Filed under:
|
|
|
|

I have a linked list of integers. When I insert a new Node I need to insert it not at the end, but in oder... i.e. 2, 4, 5, 8, 11, 12, 33, 55, 58, 102, etc. I don't think I am inserting it in the correct position. Do see what Im doing wrong?

 Node newNode = new Node(someInt);
 Node current = head;

        for(int i=0; i<count; i++){
            if(current == tail && tail.data < someInt){
                tail.next = newNode;
            }   
            if(current.data < someInt && current.next.data >= someInt){
                newNode.next = current.next;
                current.next = newNode;
            }
        }

© Stack Overflow or respective owner

Related posts about linked

Related posts about list