Dsa Midquestions Unit2
Dsa Midquestions Unit2
• Answer: A Singly Linked List (SLL) is a type of linked list where each node contains a data
field and a reference (link) to the next node in the sequence. The list is terminated with a
NULL reference in the last node.
• Logic:
o Head: Refers to the first node, and the list is traversed from the head until a node
whose next pointer is NULL is encountered (the tail node).
2. Explain the algorithm for insertion at the beginning of a Singly Linked List.
• Answer: Inserting a node at the beginning of a Singly Linked List involves creating a new
node and updating the head pointer to point to this new node.
• Logic:
return head;
o Explanation:
2. The next pointer of the new node is set to the current head.
3. The head pointer is updated to point to this new node, making it the new
first node.
3. Explain the algorithm for insertion at the end of a Singly Linked List.
• Answer: Inserting a node at the end involves traversing the list to the last node and updating
its next pointer to point to the new node.
• Logic:
if (list.head == null) {
list.head = new_node; // Set the new node as the head if the list is empty
} else {
last.next = new_node; // Set the last node's next to the new node
return list;
o Explanation:
2. Once the last node is found (its next is NULL), update its next pointer to the
new node.
• Answer: A Doubly Linked List (DLL) is a type of linked list where each node contains data
and two pointers: one pointing to the next node and another pointing to the previous node.
DLL allows traversal in both directions (forward and backward).
• Logic:
o The head node has its prev pointer set to NULL, and the tail node has its next
pointer set to NULL.
5. Explain the algorithm for insertion at the beginning of a Doubly Linked List.
• Answer: Inserting a node at the beginning involves updating the prev pointer of the current
head to point to the new node and setting the next pointer of the new node to the current
head.
• Logic:
if (head != null) {
return head;
o Explanation:
1. A new node is created, and its next is set to the current head.
2. The prev pointer of the current head is updated to point to the new node.
6. Explain the algorithm for deletion of a node from a Doubly Linked List at a specific position.
• Answer: Deletion from a specific position involves traversing the list to the required node
and updating the next and prev pointers of its surrounding nodes.
• Logic:
o Explanation:
2. Update the next pointer of the previous node and the prev pointer of the
next node to remove the current node.
7. What are the advantages and disadvantages of Circular Linked Lists (CLL)?
• Advantages:
o Efficient for cyclic tasks where the last node points back to the first node.
o Traversal from any node is possible, as any node can act as the starting point.
• Disadvantages:
o Traversal requires extra care, as the list does not have an explicit end (the tail node
points back to the head).
• Answer: Polynomials are represented using a linked list where each node represents a term
in the polynomial. Each node contains:
9. Explain the algorithm for adding two polynomials represented by linked lists.
• Answer: To add two polynomials, we traverse both lists, comparing the exponents of the
terms and adding the coefficients for terms with matching exponents.
• Logic:
return head1;
return head2;
} else {
head1.coeff += head2.coeff;
return head1;
o Explanation:
2. If the exponents are equal, add the coefficients and move to the next terms
in both polynomials.
3. If the exponents are different, move the term with the larger exponent to
the result list and continue comparing.
• Answer: A Doubly Circular Linked List is a type of linked list that combines the features of
both Doubly Linked List and Circular Linked List. Each node contains a data element and two
pointers, one pointing to the next node and another pointing to the previous node. The last
node in this list points to the first node, creating a circular structure.
• Logic:
o The next pointer of the last node points to the first node, and the prev pointer of the
first node points to the last node, allowing traversal in both directions.
2. Explain the algorithm for insertion at the beginning of a Doubly Circular Linked List.
• Answer: Insertion at the beginning involves creating a new node and adjusting pointers to
include this new node at the start of the list.
• Logic:
if (start == null) {
new_node.data = value;
start = new_node;
return;
new_node.data = value;
new_node.next = start;
new_node.prev = last;
start = new_node;
o Explanation:
1. If the list is empty, create a node where next and prev point to itself.
2. If the list is not empty, link the new node between the last node and the first
node (start), updating all pointers accordingly.
• Answer: Inserting a node at the end involves adding the new node after the current last
node and updating the circular links.
• Logic:
if (start == null) {
new_node.data = value;
start = new_node;
return;
new_node.data = value;
last.next = new_node;
start.prev = new_node;
o Explanation:
1. If the list is empty, a new node is created and its next and prev pointers
refer to itself.
2. If the list is non-empty, the prev pointer of the start node (i.e., first node)
and the next pointer of the last node (before insertion) are updated to
include the new node at the end.
3. The new node’s next points to start, and its prev points to the last node.
4. What is the time complexity of insertion in a Doubly Circular Linked List?
• Answer:
o Insertion at Beginning: The time complexity is O(1) since only a constant number of
operations are needed to adjust the pointers, irrespective of the number of nodes.
o Insertion at End: The time complexity is also O(1) since the pointer to the last node
is always available via the prev pointer of the start node, allowing constant-time
insertion.
5. Explain the algorithm for deletion of a node from a specific position in a Doubly Circular Linked
List.
• Answer: To delete a node from a specific position, we locate the node and adjust the next
and prev pointers of its neighboring nodes to remove it from the list.
• Logic:
if (curr.next == start) {
return start;
prev_1 = curr;
curr = curr.next;
start = null;
return start;
}
if (curr == start) {
prev_1 = start.prev;
start = start.next;
prev_1.next = start;
start.prev = prev_1;
prev_1.next = start;
start.prev = prev_1;
else {
prev_1.next = temp;
temp.prev = prev_1;
return start;
o Explanation:
1. The algorithm first searches for the node with the given key.
▪ If it is the first node, we adjust the prev pointer of the last node and
the next pointer of the second node.
• Advantages:
o Insertion and deletion at both ends (beginning and end) are efficient, with O(1) time
complexity.
• Disadvantages:
o Requires more space than singly linked lists due to the additional pointer (prev) in
each node.
o More complex to implement than singly linked or circular singly linked lists.