-
[LeetCode] 19. Remove Nth Node From End of List c++Coding Test/LeetCode 2022. 12. 27. 13:58728x90
- 문제
Given the head of a linked list, remove the nth node from the end of the list and return its head.
linked list에 뒤에서 n번째 노드를 제거하는 문제입니다.
- 문제 해결
class Solution { public: ListNode* removeNthFromEnd(ListNode* head, int n) { ListNode *fast = head, *slow = head; while(n--){ fast = fast->next; } if(!fast->next){ return head->next; } while(fast->next){ fast = fast->next; slow = slow->next; } slow->next = slow->next->next; return head; } };
728x90'Coding Test > LeetCode' 카테고리의 다른 글
[LeetCode] 567. Permutation in String c++ (0) 2022.12.27 [LeetCode] 3. Longest Substring Without Repeating Characters c++ (0) 2022.12.27 [LeetCode] 876. Middle of the Linked List c++ (0) 2022.12.26 [LeetCode] 557. Reverse Words in a String III c++ (0) 2022.12.26 [LeetCode] 344. Reverse String c++ (0) 2022.12.26