Linked List Flashcards
What is a linked list?
A data structure consisting of nodes that together represent a sequence.
True or False: In C++, a linked list can be implemented without using classes.
True
What keyword is used in C++ to allocate memory for a new node in a linked list?
new
Fill in the blank: In a linked list, each node contains data and a reference to the ______ node.
next
What is the primary advantage of using a linked list over an array?
Dynamic size and efficient insertions/deletions.
What is the structure of a node in a linked list without using a class?
A struct containing data and a pointer to the next node.
What is a common way to traverse a linked list?
By using a pointer to iterate through each node until the end is reached.
True or False: The last node in a linked list points to NULL.
True
What does ‘NULL’ signify in a linked list?
It indicates the end of the list.
How do you insert a new node at the beginning of a linked list?
Create the new node, set its next pointer to the current head, and update the head to the new node.
How can you delete a node from a linked list?
By adjusting the pointers of the previous node to bypass the node to be deleted.
What is the time complexity for searching an element in a linked list?
O(n)
What is the difference between singly linked lists and doubly linked lists?
Singly linked lists have nodes pointing to the next node, while doubly linked lists have nodes pointing to both next and previous nodes.
How do you find the length of a linked list?
By traversing the list and counting the nodes until NULL is reached.
What happens if you try to access the next node of the last node?
It will result in undefined behavior or a NULL pointer dereference.
What is a circular linked list?
A linked list where the last node points back to the first node, forming a circle.
True or False: You can implement a linked list using an array.
False
What is a common use case for linked lists?
Implementing stacks, queues, or dynamic memory allocation.
How do you reverse a linked list?
By changing the next pointers of each node to point to the previous node.
What is the head of a linked list?
The first node in the linked list.
How do you create a new node in a linked list without using malloc?
By using the ‘new’ keyword to allocate memory for the node.
What is the primary disadvantage of linked lists compared to arrays?
Higher memory overhead due to storing pointers.
What is the initial value of the next pointer in a new node?
NULL
How do you append a node at the end of a linked list?
Traverse to the last node and set its next pointer to the new node.