Skip to content

Commit

Permalink
Solve: Remove Nth Node From End of List
Browse files Browse the repository at this point in the history
  • Loading branch information
fkdl0048 committed Sep 30, 2024
1 parent 554aecc commit a6d660c
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions 2024/LeetCode/Remove Nth Node From End of List.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* 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* res = new ListNode(0, head); // 반환할 복사본 생성
ListNode* dummy = res;

for (int i = 0; i < n; i++){
head = head->next;
}

while (head != nullptr){
head = head->next;
dummy = dummy->next;
}

dummy->next = dummy->next->next;

return res->next;
}
};

0 comments on commit a6d660c

Please sign in to comment.