Skip to content

added leetcode solution(19) remove Nth node with code and algortihm #846

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

Merged
merged 3 commits into from
Jun 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
---
id: remove-nth-node-from-end-of-list
title: Remove nth node from end of list (LeetCode)
sidebar_label: 0019-remove-nth-node-from-end-of-list
tags:
- Two Pointers
- Linked List
description: "Given the head of a linked list, remove the nth node from the end of the list and return its head."
---
## Problem Description

| Problem Statement | Solution Link | LeetCode Profile |
| :-------------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------ | :------------------------------------------------------ |
| [Remove nth node from end of list](https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/) | [Remove nth node from end of list on LeetCode](https://leetcode.com/problems/remove-nth-node-from-end-of-list/solutions/) | [Areetra Halder](https://leetcode.com/u/areetrahalder/) |
### Problem Description

Given the head of a linked list, remove the nth node from the end of the list and return its head.

### Examples

#### Example 1

- **Input:** `head = [1,2,3,4,5], n = 2`
- **Output:** `[1,2,3,5]`

#### Example 2

- **Input:** `head = [1], n = 1`
- **Output:** `[]`

#### Example 3

- **Input:** `head = [1,2], n = 1`
- **Output:** `[1]`

### Constraints

- The number of nodes in the list is sz.
- $1 <= sz <= 30$
- $0 <= Node.val <= 100$
- $1 <= n <= sz$
### Approach
A challenging point of this question is that Linked List doesn't have index number, so we don't know which node is the last Nth node from the last.

My strategy is to create dummy pointer and create distance dummy pointer and head pointer.
### Solution Code

#### Python

```python
class Solution:
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
res = ListNode(0, head)
dummy = res

for _ in range(n):
head = head.next

while head:
head = head.next
dummy = dummy.next

dummy.next = dummy.next.next

return res.next
```
#### Java

```java
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode res = new ListNode(0, head);
ListNode dummy = res;

for (int i = 0; i < n; i++) {
head = head.next;
}

while (head != null) {
head = head.next;
dummy = dummy.next;
}

dummy.next = dummy.next.next;

return res.next;
}
}
```
#### C++

```cpp
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* res = new ListNode(0, head);
ListNode* dummy = res;

for (int i = 0; i < n; i++) {
head = head->next;
}

while (head != nullptr) {
head = head->next;
dummy = dummy->next;
}

dummy->next = dummy->next->next;

return res->next;
}
};
```
### Javascript
```javascript
var removeNthFromEnd = function(head, n) {
let res = new ListNode(0, head);
let dummy = res;

for (let i = 0; i < n; i++) {
head = head.next;
}

while (head) {
head = head.next;
dummy = dummy.next;
}

dummy.next = dummy.next.next;

return res.next;
};
```
## Step by Step Algorithm
1: Initialize variables:

- We create a dummy node res with a value of 0 and set its next pointer to the head of the original list. This dummy node helps in handling edge cases when removing the first node.
- We initialize another pointer dummy to the dummy node res. This pointer will be used to traverse the list.
```
res = ListNode(0, head)
dummy = res
```
2: Move head pointer forward by n nodes:

- We iterate n times using a for loop to advance the head pointer n nodes forward. This effectively moves head to the nth node from the beginning.
```
for _ in range(n):
head = head.next
```
3: Find the node before the node to be removed:

- We use a while loop to traverse the list with both head and dummy pointers.
- As long as head is not None, we move both head and dummy pointers one node forward in each iteration.
- After this loop, dummy will be pointing to the node right before the node to be removed.
```
while head:
head = head.next
dummy = dummy.next
```
4: Remove the nth node from the end:

- Once the loop finishes, dummy will be pointing to the node right before the node to be removed.
- We update the next pointer of the node pointed by dummy to skip the next node, effectively removing the nth node from the end.
```
dummy.next = dummy.next.next
```
5: Return the modified list:

- Finally, we return the next node after the dummy node res, which is the head of the modified list.
```
return res.next
```
This algorithm effectively removes the nth node from the end of the linked list by traversing it only once.
Loading