It is a abstract data type that represents a sequence of nodes. In other words, it can be implemented in many ways. So, here is my suggestion, try to implement it according to the situation rather than only one way.
Singly Linked List
This structure is the most common one. It is a linear data structure where each element is a separate object. Each element is called a node and every node has two parts, data and pointer to the next node. The first node is called head and the last node is called tail. The tail node points to null.
Design a Linked List
Design a Linked List with head and size attributes
Box: A smart pointer type for heap allocation. Option is a type that represents an optional value: every Option is either Some and contains a value, or None, and does not.
What is the difference between the two implementations?
The first implementation is more efficient than the second one. The second implementation is more readable than the first one.
The first implementation is more efficient because it does not need to check if the tail is None or not. The second implementation is more readable because it does not need to check if the index is 0 or not.
Doubly Linked List
This structure is similar to the singly linked list. The only difference is that each node has two pointers, one points to the next node and the other points to the previous node. The first node is called head and the last node is called tail. The head node points to null and the tail node points to null.
Why we need Doubly Linked List? Is it more efficient than Singly Linked List?
Doubly Linked List is useful when we need to traverse the linked list in both forward and backward directions. It provides a prev pointer in addition to the next pointer, which allows us to traverse the list in reverse order. However, it requires more memory than a Singly Linked List due to the extra prev pointer. In terms of time complexity, both types of linked lists have the same time complexity for most operations, such as insertion and deletion.
What are the techniques to solve the linked list problems?
For exmaple, how to find the linked list middle node, or it has a cycle or not.
Here is an example of checking if the linked list has a cycle or not.
# Definition for singly-linked list.classNode:def__init__(self,x): self.val = x self.next =NoneclassSolution:defhasCycle(self,head: Optional[ListNode]): slow = fast = headwhile fast and fast.next: slow = slow.next fast = fast.next.nextif slow == fast:returnTruereturnFalse