Skip to content

Commit 43f3df2

Browse files
authored
update md file
1 parent 9c43a52 commit 43f3df2

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
# 📝 Remove Nth Node from End of List (C++ Code)
3+
4+
Here is the C++ implementation of removing the N-th node from the end of a linked list, which is a common problem in coding interviews. This solution uses a **two-pointer technique** to efficiently find and remove the node. 🧑‍💻
5+
6+
## 🚀 Explanation
7+
1. **Edge Case Handling:** If the head is `nullptr` or `n` is non-positive, we return `nullptr`.
8+
2. **Dummy Node:** A dummy node is used to simplify edge cases where the node to be removed is the head.
9+
3. **Two Pointers:**
10+
- `fast` pointer moves ahead by `n+1` steps.
11+
- `slow` pointer starts from the dummy and moves along with `fast` until `fast` reaches the end of the list.
12+
4. **Node Removal:** The node after `slow` is the N-th node from the end. We remove it by adjusting pointers and deallocate memory.
13+
14+
### 💡 Notes:
15+
- **Time Complexity:** `O(L)` where `L` is the length of the linked list.
16+
- **Space Complexity:** `O(1)` as we only use two additional pointers.
17+
18+
---
19+
20+
I hope you found this useful! 🎉 Happy coding! ✨

0 commit comments

Comments
 (0)