My simple C++ solution


  • 0
    A
    class Solution {
    public:
        ListNode* removeNthFromEnd(ListNode* head, int n) {
            ListNode *plast, *pfront, *phead;
            plast = pfront = phead = new ListNode(0);
            plast->next = head;
            while(1)
            {
                while(n>0){
                    --n;
                    pfront = pfront->next;
                }
                if(!pfront->next)
                    break;
                plast = plast->next;
                pfront = pfront->next;
            }
            plast->next = plast->next->next;
            return phead->next;
        }
    };
    

Log in to reply
 

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