/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode *removeNthFromEnd(ListNode *head, int n) {
int len = 0;
ListNode *it = head;
while (it != nullptr) {
++len;
it = it->next;
}
int target = len - n;
ListNode *dummy = new ListNode(), *prev = dummy, *curr = head;
dummy->next = head;
for (int i = 0; i < target; ++i) {
prev = prev->next;
curr = curr->next;
}
prev->next = curr->next;
delete curr;
head = dummy->next;
delete dummy;
return head;
}
};
head
.
DFS. Recursion. Post order traversal.
Solve this coding question in one pass.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode *removeNthFromEnd(ListNode *head, int n) {
ListNode *dummy = new ListNode();
dummy->next = head;
dfs(dummy, head, n);
head = dummy->next;
delete dummy;
return head;
}
private:
void dfs(ListNode *prev, ListNode *head, int &n) {
if (head == nullptr) {
return;
}
dfs(head, head->next, n);
--n;
if (n == 0) {
prev->next = head->next;
delete head;
return;
}
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
struct Solution {
public:
ListNode *removeNthFromEnd(ListNode *head, int n) {
ListNode *dummy = new ListNode();
dummy->next = dfs(head, n);
head = dummy->next;
delete dummy;
return head;
}
private:
ListNode *dfs(ListNode *head, int &n) {
if (head == nullptr) {
return nullptr;
}
head->next = dfs(head->next, n);
--n;
if (n == 0) {
ListNode *x = head->next;
delete head;
return x;
}
return head;
}
};
head
.
Use two pointers and iteration to solve this coding question in one pass.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode *removeNthFromEnd(ListNode *head, int n) {
ListNode *dummy = new ListNode(), *it1 = dummy, *it2 = head;
dummy->next = head;
while (n > 0) {
it2 = it2->next;
--n;
}
while (it2 != nullptr) {
it1 = it1->next;
it2 = it2->next;
}
ListNode *x = it1->next;
it1->next = it1->next->next;
delete x;
head = dummy->next;
delete dummy;
return head;
}
};
head
.
My Solution Solution 1: DFS (recursion) The Key Idea for Solving This Coding Question C++ Code /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right;
Jun 6, 2025MyCircularQueueO(k)
Apr 20, 2025O(m)
Mar 4, 2025O(n)n is the length of nums.
Feb 19, 2025or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up