🌟 "Best Solution Ever! 🚀 Python, Java, C++, C, JavaScript, Go, C#, Kotlin, TypeScript, Swift 💻🌟"
💡 IntuitionWhen solving the problem of removing the nth node from the end of a linked list, the key idea is to use two pointers (fast and slow). By placing fast


## **💡 Intuition**  
When solving the problem of removing the nth node from the end of a linked list, the key idea is to use two pointers (`fast` and `slow`). By placing `fast` `n` steps ahead of `slow`, we ensure that when `fast` reaches the end, `slow` is positioned right before the node to remove.

---

## **🚀 Approach**  
1. Initialize two pointers: `fast` and `slow`, both pointing to the head of the linked list.  
2. Move the `fast` pointer `n` steps ahead.  
3. If `fast` becomes `None` after the above step, it means we need to remove the head node. Return `head.next` in this case.  
4. Otherwise, move both pointers one step at a time until `fast.next` becomes `None`.  
5. At this point, `slow.next` points to the node that needs to be removed. Adjust the `next` pointer of `slow` to skip the node.  
6. Return the modified list's head.

---

## **📊 Complexity**
- **Time Complexity**:  
  $$O(N)$$, where \(N\) is the length of the linked list. The list is traversed twice: once to advance the `fast` pointer and another to find the node to remove.
  
- **Space Complexity**:  
  $$O(1)$$, as we use only two pointers and no extra space.

---

## **📖 Dry Run Example**  
**Input**:  
`head = [1, 2, 3, 4, 5]`, `n = 2`

**Steps**:  
1. Place `fast` and `slow` at the head (`1`).  
2. Move `fast` two steps ahead. Now:  
   - `fast = 3`  
   - `slow = 1`  
3. Move both pointers one step at a time until `fast.next = None`. Now:  
   - `fast = 5`  
   - `slow = 3`  
4. Skip the node after `slow`:  
   - Update `slow.next = slow.next.next` to remove `4`.  
5. Return the modified list: `[1, 2, 3, 5]`.

---

## **💻 Code in Multiple Languages**

### **🐍 Python**  
```python
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
        slow = head
        fast = head
        for _ in range(n):
            fast = fast.next
        if not fast:
            return head.next
        while fast.next:
            fast = fast.next
            slow = slow.next
        slow.next = slow.next.next
        return head
```

🙌 If this helped you, please **upvote** ⬆️! 😊  

---

### **☕ Java**  
```java
class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode slow = head, fast = head;
        for (int i = 0; i < n; i++) fast = fast.next;
        if (fast == null) return head.next;
        while (fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        slow.next = slow.next.next;
        return head;
    }
}
```

🙌 If this helped you, please **upvote** ⬆️! 😊  

---

### **⚙️ C++**  
```cpp
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode* slow = head;
        ListNode* fast = head;
        for (int i = 0; i < n; i++) fast = fast->next;
        if (!fast) return head->next;
        while (fast->next) {
            fast = fast->next;
            slow = slow->next;
        }
        slow->next = slow->next->next;
        return head;
    }
};
```

🙌 If this helped you, please **upvote** ⬆️! 😊  

---

### **💻 C**  
```c
struct ListNode* removeNthFromEnd(struct ListNode* head, int n) {
    struct ListNode* slow = head;
    struct ListNode* fast = head;
    for (int i = 0; i < n; i++) fast = fast->next;
    if (!fast) return head->next;
    while (fast->next) {
        fast = fast->next;
        slow = slow->next;
    }
    slow->next = slow->next->next;
    return head;
}
```

🙌 If this helped you, please **upvote** ⬆️! 😊  

---

### **🌐 JavaScript**  
```javascript
var removeNthFromEnd = function(head, n) {
    let slow = head, fast = head;
    for (let i = 0; i < n; i++) fast = fast.next;
    if (!fast) return head.next;
    while (fast.next) {
        fast = fast.next;
        slow = slow.next;
    }
    slow.next = slow.next.next;
    return head;
};
```

🙌 If this helped you, please **upvote** ⬆️! 😊  

---

### **🐹 Go**  
```go
func removeNthFromEnd(head *ListNode, n int) *ListNode {
    slow, fast := head, head
    for i := 0; i < n; i++ {
        fast = fast.Next
    }
    if fast == nil {
        return head.Next
    }
    for fast.Next != nil {
        fast = fast.Next
        slow = slow.Next
    }
    slow.Next = slow.Next.Next
    return head
}
```

🙌 If this helped you, please **upvote** ⬆️! 😊  

---

### **💼 C#**  
```csharp
public class Solution {
    public ListNode RemoveNthFromEnd(ListNode head, int n) {
        ListNode slow = head, fast = head;
        for (int i = 0; i < n; i++) fast = fast.next;
        if (fast == null) return head.next;
        while (fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        slow.next = slow.next.next;
        return head;
    }
}
```

🙌 If this helped you, please **upvote** ⬆️! 😊  

---

### **📱 Kotlin**  
```kotlin
class Solution {
    fun removeNthFromEnd(head: ListNode?, n: Int): ListNode? {
        var slow = head
        var fast = head
        repeat(n) { fast = fast?.next }
        if (fast == null) return head?.next
        while (fast?.next != null) {
            fast = fast.next
            slow = slow?.next
        }
        slow?.next = slow?.next?.next
        return head
    }
}
```

🙌 If this helped you, please **upvote** ⬆️! 😊  

---

### **⌨️ TypeScript**  
```typescript
function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
    let slow = head, fast = head;
    for (let i = 0; i < n; i++) fast = fast.next;
    if (!fast) return head.next;
    while (fast.next) {
        fast = fast.next;
        slow = slow.next;
    }
    slow.next = slow.next.next;
    return head;
}
```

🙌 If this helped you, please **upvote** ⬆️! 😊  

---

### **🍎 Swift**  
```swift
class Solution {
    func removeNthFromEnd(_ head: ListNode?, _ n: Int) -> ListNode? {
        var slow = head, fast = head
        for _ in 0..<n { fast = fast?.next }
        if fast == nil { return head?.next }
        while fast?.next != nil {
            fast = fast?.next
            slow = slow?.next
        }
        slow?.next = slow?.next?.next
        return head
    }
}
```

🙌 If this helped you, please **upvote** ⬆️! 😊  

--- 

['Linked List', 'Two Pointers', 'Swift', 'Python', 'Java', 'Go', 'TypeScript', 'Python3', 'Kotlin', 'JavaScript']