Close

## Deleting duplicates from a sorted linked list

Given a sorted linked list, We have to delete all the duplicates in that list. For example, If we get the following list 1 -> 1 -> 2 -> 3 -> 3 -> 3 -> 4 -> 4 -> 5 The output should be 1 -> 2 -> 3 -> 4 ->5 To solve this…

## Sorting a linked list using bubble sort

How do we sort a linked list? In this post, we see one such method using bubble sort algorithm. We have discussed the Bubble sort algorithm on arrays in my previous post. The same algorithm is also applicable to singly linked lists as well.We first find the length of the linked list (len). The outer loop…

## Finding the nth node from the end in a linked list

Given a linked list, how do efficiently find the nth node from the end? For example  in the following linked list, 3rd node from the end is ‘6’. 2 -> 1 -> 6 -> 5 -> 9 One obvious method is to find the length of the linked list (len) by going through the list.…

## Inserting an element into a sorted linked list

Given a sorted linked list, how do we insert data into the list in proper order? This is nothing but a step in insertion sort. Simply iterate through the elements to find the position for given number and insert at that position. Here is the Java code to do that. I have used the LinkedList…

## Finding the middle of the linked list

Given a single linked list, how do you find the middle of the list?One simple method is to traverse through the list twice once to find it’s length, and second to navigate to the middle of the list by traversing length/2 nodes. Even though this method runs in O(n) time, there is still a chance…

## Finding the length of a linked list

In this post we will see an algorithm to find the length of the linked list. Here singly linked list is implemented in Object Oriented Programming (OOP) paradigm in C++. The algorithm is very simple. Just start at the beginning of the linked list, move to next until you reach the end of the list,…