[leetcode]Remove Nth Node From End of List

简单题。唯一的小技巧是使用了指向指针的指针。

class Solution {

public:

    ListNode *removeNthFromEnd(ListNode *head, int n) {

        // Start typing your C/C++ solution below

        // DO NOT write int main() function

    	ListNode * forward = head;

		ListNode ** current = &head;

        n--;

		while (n--)

		{

			forward = forward->next;

		}

		while (forward->next != 0)

		{

			forward = forward->next;

			current = &((*current)->next);

		}

		*current = (*current)->next; 

        return head;

    }

};

  

你可能感兴趣的:(LeetCode)