首页 > 代码库 > Leetcode#19 Remove Nth Node From End of List

Leetcode#19 Remove Nth Node From End of List

原题地址

 

基本模拟

 

代码:

 1 ListNode *removeNthFromEnd(ListNode *head, int n) { 2         ListNode *fast = head; 3         ListNode *slow = head; 4         ListNode *prev = NULL; 5          6         while (--n) 7             fast = fast->next; 8         while (fast->next) { 9             fast = fast->next;10             prev = slow;11             slow = slow->next;12         }13         if (!prev)14             return head->next;15         prev->next = slow->next;16         return head;17 }

 

Leetcode#19 Remove Nth Node From End of List