Solution in C++


  • 0
    P
    class Solution {
    public:
    	ListNode* removeNthFromEnd(ListNode* head, int n) 
    	{
    		ListNode* p = new ListNode(0);
    		p->next = head;
    		ListNode* q = p;
    		while (n--) head = head->next;
    		while (head != NULL) 
    		{
    			head = head->next;
    			q = q->next;
    		}
    		head = q->next;
    		q->next = q->next->next;
    		delete head;
    		head = p->next;
    		delete p;
    		return head;
    	}
    };

Log in to reply
 

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