One of the most crucial things to understand while preparing for technical interviews is Data Structures and Algorithms, questions related to linked lists are frequently asked in all the companies. Knowing how to use a linked list effectively may be quite beneficial in coding interviews. A linked list is a linear collection of data components whose order is determined by their physical location in memory rather than by their physical order. Instead, each piece serves as a stepping stone to the next. It's a data structure made up of a number of nodes that together form a sequence.

Sort a linked list based on real values given a linked list sorted by absolute values. You will be given a linked list in which node values are sorted based on their absolute values (for example, the absolute value of -9 is 9).

Example

Let's take an example to understand more about absolute sorted Linked List.

Absolute Sorted Linked List : 1 2 -3 4 -5 6 7 -8

Note that in the above example though -3 < 2, still in the increasing order sequence -3 is placed after 2 because the absolute value of -3 is 3 which is greater than 2. Same goes for -5 and -8 as well.

The Sorted Linked List will be : -8 -5 -3 1 2 4 6 7

An easy method is to go from the beginning to the end of the linked list. Examine each node you've visited to see whether it's out of sequence. If this is the case, delete it from its present location and replace it with the proper location. This is very similar to what we used to do when sorting an array using insertion sort. The time complexity of this approach is O(n*n), and it implements insertion sort for a linked list.

Original Linked List : 1 2 -3 4 -5 6 7 -8 Sorted Linked List : -8 -5 -3 1 2 4 6 7

From the above example, you can clearly see that the positive numbers will be placed in the proper sequence and the negative values are already sorted in a reversed way. A better method is to use merge sort to sort the linked list. This solution has a time complexity of O(n Log n). In O(n) time, an efficient solution may be implemented. All negative components are present in reverse order, which is a significant discovery. As we go through the list, we move any elements that are out of order to the head of the linked list. Letâ€™s look at how we can implement this solution.

We need to traverse the n-sized linked list only once. Thus, the time complexity of the program is O(n).

Space Complexity

O(1)

Frequently Asked Questions

How is the linked list represented in memory?

The linked list is kept in memory in a scattered way (locations). Each node's memory is allocated dynamically, that is, as and when it is needed. As a result, the linked list may grow as the user desires, and the size is not fixed; it can change.

How is the linked list different from the array?

An array is a grouping of items of the same data type. A linked list is an ordered collection of elements of the same type with pointers connecting each element to the next.

How do we check for palindromes in a linked list?

Traverse the linked list and save all of the linked list's elements in that string.

Examine the linked list to see if the first and last characters are equivalent. This will not make a palindrome if they are not equal at some point.

Are linked lists (FIFO) or (LIFO)?

The LinkedList class implements the List, Deque, and Queue interfaces. The first-in, first-out (FIFO) principle governs how a queue stores and removes its components. LinkedList may be used to represent a first-in, first-out (FIFO) queue since it implements the Deque interface.

Why are linked lists used over arrays?

Linked lists are more efficient than arrays in terms of memory allocation. Unlike arrays, the size of a linked list is not fixed, so it can grow or shrink as the program runs.

Conclusion

In this article, we discussed the problem related to sorting a linked list that is already sorted on absolute values. We hope that this blog has helped you enhance your problem-solving skills, and if you would like to learn more, check out our Linked List and Application of Linked List articles on Coding Ninjas Studio. Do upvote our blog to help other ninjas grow. Happy Coding!