https://leetcode.com/problems/remove-nth-node-from-end-of-list/

뒤에서 n 번째 node를 지워준다.

dummy node 를 맨앞에 하나 두고, 진행해 나가는 두 개의 node를 운용한다. first node를 n번 만큼 먼저 이동 시킨 후, second node 도 같이 진행해 나간다.

class Solution {
public:
	ListNode* removeNthFromEnd(ListNode* head, int n) {
		if (head == NULL || head->next == NULL) return NULL;
		ListNode* dummy = new ListNode(-1);
		dummy->next = head;
		ListNode* firstNode = dummy;
		for (int i = 0; i < n; i++)
		{
			if (firstNode->next == NULL) // 혹시 n 이 node 개수보다 많이 들어올까봐
				return NULL;
			firstNode = firstNode->next;
		}

		ListNode* secondNode = dummy;
		while (firstNode->next)
		{
			firstNode = firstNode->next;
			secondNode = secondNode->next;
		}
		secondNode->next = secondNode->next->next;

		return dummy->next;
	}
};

Runtime: 4 ms, faster than 83.74% of C++ online submissions for Remove Nth Node From End of List.

Memory Usage: 10.7 MB, less than 49.57% of C++ online submissions for Remove Nth Node From End of List.

Runtime: 0 ms, faster than 100.00% of C++ online submissions for Remove Nth Node From End of List.

Memory Usage: 10.8 MB, less than 49.57% of C++ online submissions for Remove Nth Node From End of List.