9 lines concise and easy understand C++ solution


  • 1
    A
    class Solution {
    public:
        ListNode* removeNthFromEnd(ListNode* head, int n) {
            ListNode* forward(head), *backhead(head);
            while(forward && n--) forward = forward->next;
            if(forward == NULL) return backhead->next;
            while(forward->next){
                forward = forward->next;
                head = head->next;
            }
            head->next = head->next->next;
            return backhead;
        }
    };

Log in to reply
 

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