-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path19.remove-nth-node-from-end-of-list.cpp
55 lines (45 loc) · 1.26 KB
/
19.remove-nth-node-from-end-of-list.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
* @lc app=leetcode id=19 lang=cpp
*
* [19] Remove Nth Node From End of List
*/
// @lc code=start
/**
* 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) {}
* };
*/
#include <algorithm>
#include <iostream>
#include <memory.h>
#include <stack>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
// 링크드 리스트 문제는 더미 노드를 만들어 놓고 처리하면 더미의 next를 반환하도록 처리하면
// 길이가 짧은 경우 등을 따로 처리해 줄 필요 없이 간단하게 처리할 수 있음.
ListNode* dummy = new ListNode(0);
dummy->next = head;
ListNode* first = dummy;
ListNode* second = dummy;
for (int i = 0; i <= n; i++) {
first = first->next;
}
while(first) {
first = first->next;
second = second->next;
}
second->next = second->next->next;
return dummy->next;
}
};
// @lc code=end