Linked Lists Flashcards
(51 cards)
What is a linked list?
A linear data structure where elements are stored in nodes, and each node points to the next.
What are the main types of linked lists?
Singly linked lists, doubly linked lists, and circular linked lists.
What is a singly linked list?
A linked list where each node points to the next node, and the last node points to null.
What is a doubly linked list?
A linked list where each node has pointers to both the next and previous nodes.
What is a circular linked list?
A linked list where the last node points back to the head node.
What is the head of a linked list?
The first node in the list.
What is the tail of a linked list?
The last node in the list.
How do you insert at the beginning of a singly linked list?
Create a new node and point it to the current head, then update head to the new node.
What is the time complexity of inserting at the head of a linked list?
O(1)
How do you insert at the end of a singly linked list?
Traverse to the last node, then set its next pointer to a new node.
What is the time complexity of inserting at the end of a singly linked list?
O(n)
How do you delete the head node in a singly linked list?
Update the head to point to the next node.
How do you delete the tail node in a singly linked list?
Traverse to the second-last node and set its next pointer to null.
What is the time complexity of searching in a linked list?
O(n)
What is the space complexity of a linked list with n nodes?
O(n)
How does a linked list differ from an array?
Linked lists use dynamic memory and support efficient insertion/deletion; arrays allow constant-time access.
What is a node in a linked list?
A basic unit containing data and one or more pointers.
What are common operations on linked lists?
Insert, delete, search, traverse, reverse.
How do you reverse a singly linked list?
Iterate through the list and rewire each node to point to the previous node.
What is a dummy head node?
A placeholder node at the beginning of a list to simplify edge cases.
What is the advantage of a dummy node?
Simplifies insertion and deletion logic, especially at the head.
What is a null pointer in linked lists?
A special value indicating the end of the list.
Can a linked list have cycles?
Yes, if a node’s next pointer links back to a previous node.
How do you detect a cycle in a linked list?
Use Floyd’s Cycle Detection Algorithm (Tortoise and Hare).