6 lines C++


  • 0
    class Solution {
    public:
        ListNode* removeNthFromEnd(ListNode* head, int n) {
            ListNode* slow(head), *fast(head);
            while(n--) fast = fast->next;
            if(!fast) return head->next;
            while(fast->next) slow = slow->next, fast = fast->next;
            slow->next = slow->next->next;
            return head;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.