Home Forums US Life 다음 5개함수 중 가장 잘 쓴 함수라고 생각되는 함수는? 이유는? 틀렸다고 생각되는 함수는? 리스트의 뒤에서 N번째 노드 지운 리스트 리턴하기 This topic has [14] replies, 0 voices, and was last updated 3 years ago by w. Now Editing “다음 5개함수 중 가장 잘 쓴 함수라고 생각되는 함수는? 이유는? 틀렸다고 생각되는 함수는? 리스트의 뒤에서 N번째 노드 지운 리스트 리턴하기” Name * Password * Email Topic Title (Maximum Length 80) ` 함수 1. ListNode* removeNthFromEnd(ListNode* head, int n) { ListNode * start = new ListNode(); start -> next = head; ListNode* fast = start; ListNode* slow = start; for(int i = 1; i <= n; ++i) fast = fast->next; while(fast->next != NULL) { fast = fast->next; slow = slow->next; } slow->next = slow->next->next; return start->next; } 함수 2. ListNode* removeNthFromEnd(ListNode* head, int n) { ListNode* slow = head; ListNode* fast = head; while (n--) fast = fast->next; if (fast == nullptr) return head->next; // return slow->next; 라고 해도 됨. while (fast->next) { slow = slow->next; fast = fast->next; } slow->next = slow->next->next; return head; } 함수 3. void removeNthFromEnd(ListNode* head, int n) { ListNode* slow = head; ListNode* fast = head; while (n--) fast = fast->next; if (fast == nullptr) head=head->next; // slow->next; 라고 해도 됨. while (fast->next) { slow = slow->next; fast = fast->next; } slow->next = slow->next->next; } 함수 4. void removeNthFromEnd(ListNode** head, int n) { ListNode* slow = *head; ListNode* fast = *head; while (n--) fast = fast->next; if (fast == nullptr) slow=slow->next; while (fast->next) { slow = slow->next; fast = fast->next; } slow->next = slow->next->next; } 함수 5. ListNode* removeNthFromEnd(ListNode* head, int n) { ListNode *pre = new ListNode(0, head), *slow = pre, *fast = pre; while (fast->next != nullptr && n-- > 0) fast = fast->next; while (fast->next != nullptr) { fast = fast->next; slow = slow->next; } slow->next = slow->next->next; return pre->next; } ` I agree to the terms of service Update List