题解 | #删除链表的倒数第n个节点#
删除链表的倒数第n个节点
http://www.nowcoder.com/practice/f95dcdafbde44b22a6d741baf71653f6
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
class Solution {
public:
/**
*
* @param head ListNode类
* @param n int整型
* @return ListNode类
*/
ListNode* removeNthFromEnd(ListNode* head, int n) {
// write code here
ListNode *r,*l;
r=l=head;
int m=0,k=0;
while(r){
++m;
if(m<=n+1){
k=n+1-m;
r=r->next;
}
else {
r=r->next;
l=l->next;
}
}
if(k==1){
head=head->next;
}
else {
r=l->next;
l->next=r->next;
}
return head;
}
};
曼迪匹艾公司福利 121人发布