https://leetcode.com/problems/remove-nth-node-from-end-of-list/
1. Use one-pass:
//4 ms 10.4 MB/*** 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) {if(!head || (!head->next && n == 1)) return NULL;ListNode* dummy = new ListNode(-1);dummy->next = head;ListNode* curr1 = head;ListNode* curr2 = head;ListNode* prev = dummy;for(int i = 1; i < n; i++)curr1 = curr1->next;while(curr1->next){prev = curr2;curr1 = curr1->next;curr2 = curr2->next;}prev->next = curr2->next;return dummy->next;}};
