Linked Lists Flashcards

(51 cards)

1
Q

What is a linked list?

A

A linear data structure where elements are stored in nodes, and each node points to the next.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the main types of linked lists?

A

Singly linked lists, doubly linked lists, and circular linked lists.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a singly linked list?

A

A linked list where each node points to the next node, and the last node points to null.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a doubly linked list?

A

A linked list where each node has pointers to both the next and previous nodes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a circular linked list?

A

A linked list where the last node points back to the head node.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the head of a linked list?

A

The first node in the list.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the tail of a linked list?

A

The last node in the list.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you insert at the beginning of a singly linked list?

A

Create a new node and point it to the current head, then update head to the new node.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the time complexity of inserting at the head of a linked list?

A

O(1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you insert at the end of a singly linked list?

A

Traverse to the last node, then set its next pointer to a new node.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the time complexity of inserting at the end of a singly linked list?

A

O(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you delete the head node in a singly linked list?

A

Update the head to point to the next node.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you delete the tail node in a singly linked list?

A

Traverse to the second-last node and set its next pointer to null.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the time complexity of searching in a linked list?

A

O(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the space complexity of a linked list with n nodes?

A

O(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How does a linked list differ from an array?

A

Linked lists use dynamic memory and support efficient insertion/deletion; arrays allow constant-time access.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is a node in a linked list?

A

A basic unit containing data and one or more pointers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What are common operations on linked lists?

A

Insert, delete, search, traverse, reverse.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How do you reverse a singly linked list?

A

Iterate through the list and rewire each node to point to the previous node.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What is a dummy head node?

A

A placeholder node at the beginning of a list to simplify edge cases.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What is the advantage of a dummy node?

A

Simplifies insertion and deletion logic, especially at the head.

22
Q

What is a null pointer in linked lists?

A

A special value indicating the end of the list.

23
Q

Can a linked list have cycles?

A

Yes, if a node’s next pointer links back to a previous node.

24
Q

How do you detect a cycle in a linked list?

A

Use Floyd’s Cycle Detection Algorithm (Tortoise and Hare).

25
What is the time complexity of cycle detection using Floyd’s algorithm?
O(n)
26
What is the space complexity of Floyd’s cycle detection algorithm?
O(1)
27
What is a sentinel node?
Another name for a dummy node used as a placeholder.
28
What is the benefit of a circular linked list?
Allows continuous traversal from tail to head.
29
What is a use case for a doubly linked list?
Efficient backward traversal or deletion without needing to traverse the list.
30
What is the time complexity of inserting at a known position in a doubly linked list?
O(1)
31
What is the time complexity of inserting at a known position in a singly linked list?
O(1) if the node is known, otherwise O(n)
32
How is memory allocated in linked lists?
Each node is allocated dynamically on the heap.
33
What is a memory leak in the context of linked lists?
Forgetting to free memory for removed nodes.
34
How do you traverse a linked list?
Start from the head and follow next pointers until null.
35
How do you find the length of a linked list?
Traverse the list, incrementing a counter until reaching null.
36
How do you merge two sorted linked lists?
Use a two-pointer approach to build a new sorted list.
37
How do you implement a stack with a linked list?
Insert and delete from the head of the list.
38
How do you implement a queue with a linked list?
Insert at the tail and delete from the head.
39
What is the advantage of a linked list over an array in dynamic scenarios?
Linked lists can grow and shrink in size without reallocating memory.
40
What is the main disadvantage of linked lists?
No constant-time random access; more memory usage due to pointers.
41
What causes segmentation faults in linked lists?
Accessing or modifying memory through an invalid or null pointer.
42
How do you remove duplicates from an unsorted linked list?
Use a hash set to track seen values.
43
What is a common interview question involving linked lists?
Detect and remove cycles, or find the intersection point of two lists.
44
How do you find the middle of a linked list?
Use two pointers: one moves twice as fast as the other.
45
What is the time complexity of finding the middle element in a linked list?
O(n)
46
How do you find the nth node from the end?
Use two pointers with a gap of n between them.
47
Can you sort a linked list?
Yes, using merge sort which works efficiently with linked lists.
48
What is the time complexity of merge sort on linked lists?
O(n log n)
49
What is the benefit of using merge sort on a linked list over quicksort?
No random access is needed, and merge sort is stable.
50
What is a circular doubly linked list?
A doubly linked list where the tail connects back to the head and vice versa.
51
What are sentinel values used for in linked lists?
To simplify boundary conditions during traversal or modification.