Repository navigation
Fix tail update on insertion in CircularLinkedList - #583
Conversation
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 0 |
| Duplication | 0 |
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
There was a problem hiding this comment.
🟡 Changes recommended
The new test is declared outside the CircularLinkedListTests class at namespace scope, causing C# error CS0116 and preventing the test project from compiling.
1 open issue
What changed in this PR
This PR fixes CircularLinkedList<T>.InsertAfter so inserting after the tail updates the tail pointer and preserves the existing head.
Changes:
- Reassigns
tailto the new node when inserting after the current tail. - Adds a regression test checking head preservation, node order, and circular linkage.
| File | Description |
|---|---|
| DataStructures/LinkedList/CircularLinkedList/CircularLinkedList.cs | Updates the tail pointer after tail insertion. |
| DataStructures.Tests/LinkedList/CircularLinkedListTests.cs | Adds a regression test for insertion after a single-node list’s tail. |
Review effort: Balanced
0a212c7 to
f979a77
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #583 +/- ##
==========================================
- Coverage 96.96% 96.96% -0.01%
==========================================
Files 300 300
Lines 12598 12602 +4
Branches 1842 1843 +1
==========================================
+ Hits 12216 12219 +3
Misses 242 242
- Partials 140 141 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
You are welcome :) |


Summary of Changes
Fixed a bug in CircularLinkedList.InsertAfter where inserting a node after the current tail failed to update the tail pointer to the newly created node.
Motivation & Context
In a circular linked list tracked by a single tail pointer, the head of the list is defined as tail.Next. When calling InsertAfter(value, data) where value belongs to the current tail, the new node was correctly linked after tail, but tail itself was never reassigned to newNode. Consequently, the list structure became inverted: the newly inserted element was treated as the new head (tail.Next), and the old tail remained marked as the end of the list.
Checklist
[x] I have performed a self-review of my code
[x] My code follows the style guidelines of this project
[x] I have added tests that prove my fix is effective or that my feature works
[x] New and existing unit tests pass locally with my changes
[x] Comments in areas I changed are up to date
[ ] I have added comments to hard-to-understand areas of my code
[ ] I have made corresponding changes to the README.md