Skip to content

Add cycle creation utility and detection tests for Linked List (Fixes #583) #2052

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sowmya-rebbavarapu
Copy link

Description:
This pull request adds the ability to create a cycle in the linked list by linking the tail node to any node at a specified position. It includes:

A utility function createCycle(list, position) that links the last node to the node at the given position to form a cycle.

Unit tests to verify cycle creation and detection using the existing detectCycle function.

Tests cover normal cases, invalid positions, and no-cycle scenarios.

This addresses the feature request in issue #583 opened by @chrisVillanueva, enabling users to create and detect cycles within linked lists for more comprehensive linked list operations.

Please review the implementation and tests. Looking forward to your feedback!

@lazarljubenovic
Copy link

That's not a linked list anymore.

Copy link

@Imran-imtiaz48 Imran-imtiaz48 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job on this test suite! Here are some specific observations and suggestions:

Positives

  • Test Coverage: The tests cover a good range of scenarios, including:
    • No cycle in the linked list
    • Correct cycle creation and detection
    • Handling invalid positions for cycle creation (out-of-bounds and negative)
  • Readability: Test cases are clear and descriptive, making it easy to understand the intention behind each scenario.
  • Setup: Using beforeEach to initialize a new LinkedList keeps tests isolated and reliable.

Suggestions/Improvements

  1. Edge Cases:
    Consider adding tests for these edge cases:

    • An empty list (should return false for cycle detection).
    • A list with a single node (cycle vs. no cycle).
    • Creating a cycle at position 0 (head node).
  2. Assertions:
    If createCycle returns a value (such as success/failure), consider asserting it as well to make the effect of the function explicit.

  3. Comments:
    Your current comments are helpful! If possible, expand them a bit for maintainability, especially in less obvious scenarios.

  4. Consistency:
    The pattern and naming are consistent and align with best practices for unit testing in JavaScript.


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants