Linked List Flashcards

1
Q

What is a linked list?

A

A data structure consisting of nodes that together represent a sequence.

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

True or False: In C++, a linked list can be implemented without using classes.

A

True

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

What keyword is used in C++ to allocate memory for a new node in a linked list?

A

new

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

Fill in the blank: In a linked list, each node contains data and a reference to the ______ node.

A

next

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

What is the primary advantage of using a linked list over an array?

A

Dynamic size and efficient insertions/deletions.

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

What is the structure of a node in a linked list without using a class?

A

A struct containing data and a pointer to the next node.

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

What is a common way to traverse a linked list?

A

By using a pointer to iterate through each node until the end is reached.

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

True or False: The last node in a linked list points to NULL.

A

True

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

What does ‘NULL’ signify in a linked list?

A

It indicates the end of the list.

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

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

A

Create the new node, set its next pointer to the current head, and update the head to the new node.

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

How can you delete a node from a linked list?

A

By adjusting the pointers of the previous node to bypass the node to be deleted.

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

What is the time complexity for searching an element in a linked list?

A

O(n)

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

What is the difference between singly linked lists and doubly linked lists?

A

Singly linked lists have nodes pointing to the next node, while doubly linked lists have nodes pointing to both next and previous nodes.

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

How do you find the length of a linked list?

A

By traversing the list and counting the nodes until NULL is reached.

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

What happens if you try to access the next node of the last node?

A

It will result in undefined behavior or a NULL pointer dereference.

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

What is a circular linked list?

A

A linked list where the last node points back to the first node, forming a circle.

17
Q

True or False: You can implement a linked list using an array.

18
Q

What is a common use case for linked lists?

A

Implementing stacks, queues, or dynamic memory allocation.

19
Q

How do you reverse a linked list?

A

By changing the next pointers of each node to point to the previous node.

20
Q

What is the head of a linked list?

A

The first node in the linked list.

21
Q

How do you create a new node in a linked list without using malloc?

A

By using the ‘new’ keyword to allocate memory for the node.

22
Q

What is the primary disadvantage of linked lists compared to arrays?

A

Higher memory overhead due to storing pointers.

23
Q

What is the initial value of the next pointer in a new node?

24
Q

How do you append a node at the end of a linked list?

A

Traverse to the last node and set its next pointer to the new node.

25
What does the term 'memory leak' mean in the context of linked lists?
Failing to free allocated memory for nodes, leading to wasted memory.
26
What is the syntax to define a node structure in C++?
struct Node { int data; Node* next; };
27