4ms beats 96.44% c++


  • 0
    T
    class Solution {
    public:
        ListNode* removeNthFromEnd(ListNode* head, int n) {
            int len=0;
            ListNode* tmphead=head;
            while (tmphead!=NULL)
            {
                len++;
                tmphead=tmphead->next;
            }
            int split=len-n;
            tmphead=head;
            for(int i=1;i<split;i++)
            {
                tmphead=tmphead->next;
            }
            if (n==len)
                head=head->next;
            else
                tmphead->next=tmphead->next->next;
            return head;
    
        }
    };
    

Log in to reply
 

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