Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if either linked list head is null.
DSA Java
if (headA == [1] || headB == null) { return null; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of null for empty list check.
Checking only one list head instead of both.
✗ Incorrect
We check if headA or headB is null to handle empty lists before proceeding.
2fill in blank
mediumComplete the code to find the length of a linked list.
DSA Java
int length = 0; ListNode current = head; while (current != [1]) { length++; current = current.next; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using current.next instead of null in condition.
Using head instead of current in the loop condition.
✗ Incorrect
We traverse until current is null, meaning end of the list.
3fill in blank
hardFix the error in advancing the longer list pointer by the difference in lengths.
DSA Java
while (diff > 0) { [1] = [1].next; diff--; }
Attempts:
3 left
💡 Hint
Common Mistakes
Advancing the shorter list pointer instead.
Using headA or headB directly without assigning to longer.
✗ Incorrect
We move the pointer of the longer list forward to align both lists.
4fill in blank
hardFill both blanks to check if two nodes are the same and move both pointers forward.
DSA Java
while (longer != null && shorter != null) { if (longer [1] shorter) { return longer; } longer = longer.[2]; shorter = shorter.next; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using equals() which checks value equality, not reference.
Using 'prev' which is not defined in singly linked lists.
✗ Incorrect
We compare nodes by reference using '==' and move to next nodes.
5fill in blank
hardFill both blanks to initialize pointers and calculate length difference.
DSA Java
ListNode longer = headA; ListNode shorter = headB; int lenA = 0, lenB = 0; ListNode currentA = headA; ListNode currentB = headB; while (currentA != [1]) { lenA++; currentA = currentA.next; } while (currentB != [2]) { lenB++; currentB = currentB.next; } int diff = lenA - lenB; if (diff < 0) { longer = headB; shorter = headA; diff = -diff; } while (diff > 0) { longer = longer.next; diff--; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using headA or headB in loop condition instead of current pointers.
Using an undefined variable 'current' in loop condition.
✗ Incorrect
We traverse until currentA and currentB reach null to count lengths.
