8ms c++ solution


  • 0
    N
    class Solution {
    public:
        ListNode* removeNthFromEnd(ListNode* head, int n) {
            if(head==NULL) return NULL;
            ListNode* start1=head;
            ListNode* start2=head;
            int i;
            ListNode *Temp=NULL;
            for(i=1;i<=n;i++) {
                start2=start2->next;
            }
            if(start2==NULL) {
                Temp=start1->next;
                delete(start1);
                return Temp;
            }
            while((start2->next)!=NULL) {
                start1=start1->next;
                start2=start2->next;
            }
            Temp=(start1->next)->next;
            delete(start1->next);
            start1->next=Temp;
            return head;
        }
    };

Log in to reply
 

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