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:
- You should create two pointers. The front pointer starts from the head of the list. The primary purpose of the front pointer is to stop the loop when needed. The back pointer starts as a null. The reason for that is that it is no assigned to any element yet. The primary purpose of the back pointer is to connect the previous element with the new one;

//Put out selection to the start of the list
ListNode frontPointer = head;
//Temporary node to track item to be attached with
ListNode backPointer = null;
- Start “while loop.” The primary purpose of that loop is to find the right place for a new element in the linked list. It will loop until the front pointer is not null, and its value is less than the value of the new element. That means if the front pointer is more than a new element, we have found the right place, and we can switch the connections in the list;
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;
}
- Switch the connections. After the loop stops, our front and back pointers suppose to be in the right place. We can connect our new element to the current element. And the previous element’s tail to the new element.
//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;
- Set our current element to the first element of the list. So we can loop through the list and check the result.
//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

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:
- Search for an element in the Linked List in Java
- Deleting an item in the Linked List in Java (Android Studio)
- Adding items to the Linked List in Java.
- Getting the length (size) of Linked List in Java (Android Studio)
- Two ways to implement a Linked List in Java. Example.
- Reversing the linked list.
- Middle of the linked list example.
- Find the nth element from the last in Linked List (Java)
- Removing duplicates from the linked list
Leave Any Questions Related To This Article Below!