diff --git a/2024/LeetCode/Remove Nth Node From End of List.cpp b/2024/LeetCode/Remove Nth Node From End of List.cpp new file mode 100644 index 0000000..e79ec96 --- /dev/null +++ b/2024/LeetCode/Remove Nth Node From End of List.cpp @@ -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; + } +}; \ No newline at end of file