Skip to content

Conversation

@Samya-Agg
Copy link
Contributor

@Samya-Agg Samya-Agg commented Oct 28, 2025

C++ code to reorder all odd-positioned nodes together followed by all even-positioned nodes.

-Maintain two pointers:

odd → points to the first node.

even → points to the second node.

-Move odd and even pointers alternatively.

-Then, link the end of the odd list to the start of the even list.

Summary by CodeRabbit

Release Notes

  • New Features
    • Added a linked list implementation with odd-even position reordering capability. Rearranges list elements by separating odd-positioned nodes from even-positioned nodes, placing odd-positioned nodes first while preserving relative order within each group. Includes sample demonstration.
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 28, 2025

Walkthrough

A new C++ program implements a linked list with an odd-even reordering operation. The LinkedList class provides insertion, reordering, and printing functionality. The oddEven method rearranges nodes so odd-positioned nodes appear first, followed by even-positioned nodes, each group preserving relative order.

Changes

Cohort / File(s) Summary
Linked List Odd-Even Implementation
Q) Linked List/odd-even-linkedlist.cpp
New file introducing Node struct with data and next pointer, LinkedList class with insert/oddEven/print methods, and main demonstrating insertion of values 1–5 and odd-even reordering

Sequence Diagram

sequenceDiagram participant Main participant LinkedList participant Node Main->>LinkedList: insert(1..5) LinkedList->>Node: Create nodes and link Note over LinkedList: head → 1 → 2 → 3 → 4 → 5 Main->>LinkedList: print() LinkedList->>Main: Display: 1 → 2 → 3 → 4 → 5 Main->>LinkedList: oddEven() rect rgb(230, 245, 230) Note over LinkedList: Separate odd positions (1,3,5) from even (2,4) LinkedList->>LinkedList: Rearrange: odd nodes linked together LinkedList->>LinkedList: Keep even nodes linked separately LinkedList->>LinkedList: Concatenate odd list with even list end Note over LinkedList: head → 1 → 3 → 5 → 2 → 4 Main->>LinkedList: print() LinkedList->>Main: Display: 1 → 3 → 5 → 2 → 4 
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

  • Straightforward linked list implementation with clear structure
  • Single file with self-contained logic
  • Algorithm logic is standard list manipulation with no complex edge cases apparent
  • Good candidate for quick verification of correctness in oddEven pointer reassignments

Suggested labels

New Question

Poem

🐰 Nodes hop forth in odd-even grace,
Each position finds its place,
Odd then even, linked with care,
A rabbit's list beyond compare! 🌟

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title "odd-even linked list" directly corresponds to the main change in the pull request, which is the addition of a complete C++ implementation for reordering a singly linked list so that nodes at odd positions appear first, followed by nodes at even positions. The title is concise, clear, and specific enough that a developer scanning pull request history would immediately understand the primary contribution. It accurately captures the core functionality being added without unnecessary embellishment or vague terminology.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (4)
Q) Linked List/odd-even-linkedlist.cpp (4)

3-4: Consider avoiding using namespace std; in global scope.

While acceptable for small programs, it's better practice to use explicit std:: prefixes or limit the scope of the using declaration to avoid name collisions.


14-14: Consider making head private to preserve encapsulation.

Exposing head as public allows external code to directly manipulate the internal structure, potentially breaking invariants. Making it private and providing controlled access through methods would be more robust.


18-28: Consider maintaining a tail pointer for O(1) insertion.

The current implementation traverses the entire list for each insertion (O(n)). Maintaining a tail pointer would allow O(1) append operations.


47-54: Consider making this method const.

Since print() doesn't modify the list, marking it as const would better express intent and allow it to be called on const LinkedList objects.

- void print() { + void print() const {
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f1c9944 and 8d24361.

📒 Files selected for processing (1)
  • Q) Linked List/odd-even-linkedlist.cpp (1 hunks)
🧰 Additional context used
🪛 Clang (14.0.6)
Q) Linked List/odd-even-linkedlist.cpp

[error] 3-3: 'iostream' file not found

(clang-diagnostic-error)

🔇 Additional comments (3)
Q) Linked List/odd-even-linkedlist.cpp (3)

6-10: LGTM!

The Node struct is well-implemented with proper initialization.


30-45: LGTM!

The oddEven algorithm is correctly implemented. It properly handles edge cases (empty, single node) and correctly rearranges the linked list to group odd-positioned nodes before even-positioned nodes while preserving relative order.


57-74: LGTM!

The main function clearly demonstrates the odd-even reordering functionality with a simple test case.

Comment on lines +12 to +16
class LinkedList {
public:
Node* head;

LinkedList() : head(nullptr) {}
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

Add destructor and implement rule of five to prevent memory leaks and undefined behavior.

The class manages dynamic memory but lacks:

  • A destructor to free allocated nodes (causes memory leak)
  • Copy constructor and copy assignment operator (can cause double-free)
  • Move constructor and move assignment operator (optional but recommended)

Add a destructor to properly clean up:

+ ~LinkedList() { + Node* current = head; + while (current) { + Node* next = current->next; + delete current; + current = next; + } + } + + // Prevent copying (or implement proper deep copy) + LinkedList(const LinkedList&) = delete; + LinkedList& operator=(const LinkedList&) = delete;
🤖 Prompt for AI Agents
In Q) Linked List/odd-even-linkedlist.cpp around lines 12 to 16, the LinkedList class manages heap nodes but has no destructor or special-member functions; implement the Rule of Five: add a destructor that iterates the list and deletes all nodes, implement a deep-copy copy constructor and copy assignment operator that allocate new nodes copying values and preserve list order (use the copy-and-swap idiom for assignment), and implement move constructor and move assignment that take ownership of the head pointer and set the source.head to nullptr (mark moves noexcept). Ensure all paths free previous resources before reassigning to avoid leaks and double-free. 
@AnkitMajee
Copy link
Owner

@Samya-Agg Merged

@AnkitMajee AnkitMajee merged commit 94924d9 into AnkitMajee:main Oct 29, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

2 participants