DSA-Bootcamp-Java icon indicating copy to clipboard operation
DSA-Bootcamp-Java copied to clipboard

Fix: Correct CLL display and insertion logic

Open krishnasrivaibhav2027 opened this issue 5 months ago • 0 comments

This commit addresses two issues in the Circularly Linked List (CLL) implementation:

  1. Potential infinite loop in display(): The display() method had a conditional check (if (node.next != null)) before advancing the node pointer. This check was redundant in a CLL and could lead to an infinite loop if node.next was head (e.g. in a single-element list). The condition has been removed, and node = node.next is now unconditional within the loop.

  2. Bug in insert() for single-element lists: When inserting the first element into an empty list, the node.next = head assignment was missing, meaning the list wasn't actually made circular. This caused a NullPointerException when display() was called on such a list. The insert() method has been corrected to ensure node.next = head is set when the first element is added.

A main method with test cases for the display() method (empty list, single-element list, multi-element list) has been added to CLL.java to verify the fixes and ensure correct behavior.

krishnasrivaibhav2027 avatar May 29 '25 16:05 krishnasrivaibhav2027