Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare the previous node pointer in a doubly linked list node.
DSA Java
class Node { int data; Node next; Node [1]; Node(int data) { this.data = data; next = null; prev = null; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-standard or longer names like 'previousNode' instead of 'prev'.
Confusing 'next' and 'prev' pointers.
✗ Incorrect
The standard name for the pointer to the previous node in a doubly linked list is 'prev'.
2fill in blank
mediumComplete the constructor to initialize the data field of the node.
DSA Java
class Node { int data; Node next; Node prev; Node(int [1]) { this.data = data; next = null; prev = null; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different parameter name and causing confusion in assignment.
Forgetting to initialize the data field.
✗ Incorrect
The constructor parameter should match the variable used to initialize the data field, which is 'data'.
3fill in blank
hardFix the error in the method to set the previous node pointer.
DSA Java
public void setPrev(Node [1]) {
this.prev = prevNode;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for the parameter and the variable inside the method.
Causing a compilation error due to undefined variable.
✗ Incorrect
The parameter name must match the variable used in the method body. Here, 'prevNode' is used, so the parameter must be named 'prevNode'.
4fill in blank
hardFill both blanks to complete the method that returns the next node.
DSA Java
public Node [1]() { return [2]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up 'prev' and 'next' pointers.
Naming the method incorrectly.
✗ Incorrect
The method to get the next node is named 'getNext' and it returns the 'next' pointer.
5fill in blank
hardFill all three blanks to complete the method that inserts a new node after the current node.
DSA Java
public void insertAfter(Node [1]) { [1].prev = this; [1].[3] = this.[3]; if (this.[3] != null) { this.[3].prev = [1]; } [2] = [1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing 'next' and 'prev' pointers during insertion.
Forgetting to update the previous pointer of the next node.
✗ Incorrect
The new node parameter is 'newNode'. We set 'this.next' to 'newNode' and use 'next' to check and update the next node's prev pointer.
