给你一个链表,删除链表的倒数第 n
个结点,并且返回链表的头结点。
* struct ListNode { * int val; * struct ListNode *next; * }; 这段代码运行结果: 输入[1,2,3,4,5] 2 输出[1,2,3,4] 预期结果[1,2,3,5] struct ListNode* removeNthFromEnd(struct ListNode* head, int n) { struct ListNode* pCurrent=head; struct ListNode* pFast=head; struct ListNode* pHead = head; for(int i=0;i<n;i++) pFast= pFast->next; while(pFast!=NULL) { pFast=pFast->next; pCurrent=pCurrent->next; } pCurrent->next = pCurrent->next->next; return pHead; }