How to insert an item in the sorted Linked List in Java. – IT Blog
IT Blog
Boris IT Expert

How to insert an item in the sorted Linked List in Java.

Boris~November 2, 2020 /Linked List

If you want to insert an item in the sorted linked list in Java, you should follow several steps. This solution works if we work with the sorted linked list. The fundamental idea to do with the pointers: the front pointer and the back pointer. Set the minimum interval between them, which is one element. Move both of them with the help of the “while loop” until the front pointer meets the needed place.

Insert an item in the sorted Linked List. Example of the implementation in Java.

To insert the element in the sorted linked list, you have to follow several steps:

//Put out selection to the start of the list
        ListNode frontPointer = head;
//Temporary node to track item to be attached with
        ListNode backPointer = null;
           while (frontPointer != null && frontPointer.data < newNode.data) {
//Moving "end of tail" to the next item if it is still less than data in needed item
            backPointer = frontPointer;
//Moving selected item to check the value of next item in the list
            frontPointer = frontPointer.next;
        }
//When we exit the loop means we found the place we should put new item
//It suppose to be more than temporary and less than selected data which goes after temporary
//We connect "tail" of new item to current item
        newNode.next = frontPointer;
//Connect temporary "tail" to the new item
        backPointer.next = newNode;
//Going back to the head to log the list
     frontPointer = head;
while (frontPointer != null) {
//Showing data of each item in the logs
    Log.d("linkedList", "-> " + frontPointer.data);
    frontPointer = frontPointer.next;
}

Detailed schema of inserting the element

insert item in sorted linked list java
Full code solution example.
private void insertItemInSortedList() {
        //You have to create sorted linked list first
        creatingLinkedList();

        //Put out selection to the start of the list
        ListNode frontPointer = head;
        //Temporary node to track item to be attached with
        ListNode backPointer = null;
        //Newnode which we need to insert in the list
        ListNode newNode = new ListNode(15);
        //Loop until come to last item AND until the value of selected item is less than data in new item

        while (frontPointer != null && frontPointer.data < newNode.data) {
            //Moving "end of tail" to the next item if it is still less than data in needed item
            backPointer = frontPointer;
            //Moving selected item to check the value of next item in the list
            frontPointer = frontPointer.next;
        }

        //When we exit the loop means we found the place we should put new item
        //It suppose to be more than temporary and less than selected data which goes after temporary
        //We connect "tail" of new item to current item
        newNode.next = frontPointer;
        //Connect temporary "tail" to the new item
        backPointer.next = newNode;

        //Going back to the head to log the list
        frontPointer = head;
        while (frontPointer != null) {
            //Showing data of each item in the logs
            Log.d("linkedList", "-> " + frontPointer.data);
            frontPointer = frontPointer.next;
        }
        Log.d("linkedList", "-> " + null);
    }

In this article, you figured out the easiest way to remove duplicates from the sorted linked list java.

Full code and more examples in my facebook group.

Useful links:

Leave Any Questions Related To This Article Below!