题目描述:给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点。
代码:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode**t1=&head,*t2=head;
for(int i=1;i<n;i++){
t2=t2->next;
}
while(t2->next!=NULL){
t1=&((*t1)->next);
t2=t2->next;
}
*t1=(*t1)->next;
return head;
}
};