how to find first node (head) of circular linked list - data-structures

I have question regarding circular linked list,
1. How to find first node (head) of circular linked list?
2. what is the practical use of circular linked list, why we need it?
i have gone through lots of forums and website but did not find up to the mark answers.
Thanks all for your time.

There is no way. You normally maintain a head pointer along with the nodes.

Related

Why LRU caches use doubly link list and not singly link list?

I have been trying to understand why LRU caches use doubly link list and not singly link list?
If i go by the time complexities they both have same for insertion , updation and deletion.
Here is the Cheat sheet
Is it because the two ways pointers in DLL is used for easier move of nodes to rear or front ??
The idea behind LRU cache implementation using list (DLL/SLL) is to move the recently used page(node) to the front.
This involves a lot of shifting, say the node is in the middle of the list (DLL/SLL), you'd have to remove the node, rearrange the next pointer of the previous node.
Now in this case, if we use Singly Linked List we'd have to maintain the previous node of the most recently accessed node.
This operation is not necessary if we use Doubly Linked List as it already maintains the previous and next pointer.
The catch here is accessing that latest node, for which we use a hashtable giving us access to that node in O(1).
To remove a targeted node from a linked list, you need to modify the other nodes that point to it.
In a doubly-linked list, the targeted node has pointers to these other nodes, so it's easy to find them.
In a single-linked list, the targeted node does not have a pointer to the other node that points to it. You still need to modify that node, though, so you'd have to search for it.

What is the right implementation for a singly linked list?

I'm preparing for coding interviews, and studying Singly Linked Lists, I found 2 books implementing them in 2 different ways:
The HEAD pointer is pointing to a node which element is None and its reference is the actual first node. (If the Linked list is empty, HEAD and TAIL are pointing to this particular node)
The HEAD pointer is pointing to the actual first node directly. (If the Linked list is empty, HEAD and TAIL are pointing to None Object)
So, which is the best implementation to know for coding interviews?
I would prefer to use the second approach. The first approach has one node when actually we have zero entries in the list. For me its easier to understand if a list is empty or not in the second case. If someone asks you in the interview why the choice of that strategy just make sure every edge case is covered in your answer.

Follow up on detecting loop in linked list

So there are several questions on how to detect a loop in a linked list. Here is one example. My question is, why do all these algorithms use two pointers? Couldn't you just loop through with one pointer and mark the nodes as visited, and when you come to a node you've already visited or reach the end of the linked list (next = null), then you know there is no loop?
It's because to
mark the nodes as visited
you need either extra space in the nodes themselves to do it, or an auxiliary data structure whose size will increase with that of the list, whereas the two-pointer solutions require only enough extra space for one more pointer.
[EDITED to add:] ... And, perhaps, also because the two-pointer solutions are clever and people like clever solutions to things.

difference between double-ended linked lists and doubly-linked list

I don't understand difference between a double-ended and doubly-linked list.
What is the major difference between the two?
In a doubly linked list, each node has two pointers. One towards its next node and another one towards its previous node.
In a double-ended linked list, each node has just one pointer which points to its next node. Its difference from the single-ended linked list is that instead of just one "head" node, it contains two pointers of this kind ("first" and "last"), so someone is able to insert elements to list from both ends of it.
(Last picture isn't that clear, but it catches the point of the two ends.)
A double ended list is similar to an ordinary linked list, but it has one additional features: a reference to the last link as well as to the first.
In a doubly linked list each link has two references to other links instead of one. The first is to the next link, as in ordinary lists. The second is to the previous link.
A doubly linked list is a list where the elements have pointers to both the element before and after in the list.
A double ended list is from my understanding the same as a deque. That is a queue from which you can add and remove items from both the top and the bottom.
Single linked it's one way direction and it uses less memory and the complexity of insertion is O(n). While the double linked is a two way direction (next and previous), it uses more memory than the single list and the complexity of insertion and deletion is O(n).
In both lists, there are two pointers from front and end. But the Double-ended list can't move backward, only to forward while the Doubly Linked list can move both forward & backward.

can we construct a binary tree with singly linked list

I have googled a lot and none of them showed how to create a binary tree with singly linked list.
Is it even possible to create one ?! I remember that I Have read somewhere that Binary trees can be created using singly linked list.
You can represent a binary tree as an array. If the only direction you want to go in your tree is root-to-leaf, then you could, in theory, use a singly linked list instead of the array.
This would result, however, in a huge performance loss as you will have to go pointer chasing instead of just jumping directly to the next node, as you do in an array.
I find it hard to think of a scenario where you would actually do that, but its possible in principle.

Resources