Description
If you shed tears when you miss the sun, you also miss the stars.
n
个结点 给你一个链表,删除链表的倒数第 n
个结点,并且返回链表的头结点。
输入:head = [1,2,3,4,5], n = 2
输出:[1,2,3,5]
输入:head = [1], n = 1
输出:[]
输入:head = [1,2], n = 1
输出:[1]
提示:
sz
<= 30Node.val
<= 100n
<= sz
感觉应该是第二次做这道题了,这次果断用双指针。虽说是双指针,实际上建了4个,但最有用的是那两个。同时要注意加个哨兵根节点,这样进行删除操作时更加方便,可以定位到前一个节点。核心是让first节点先从哨兵节点跑n+1步(head节点开始跑n步),然后再用一个循环定位到倒数n-1个节点,把倒数第n个删了,最后也只需要返回哨兵节点的下一个节点作为根节点。
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* first = head;
ListNode* dump = new ListNode(101);
ListNode* last = dump;
last->next = head;
for(int i = 0; i < n; i++){
first = first->next;
}
while(first){
first = first->next;
last = last->next;
}
ListNode* tmp = last;
last = last->next;
tmp->next = last->next;
return dump->next;
}
};
This app can be installed on your PC or mobile device. This will allow this web app to look and behave like any other installed app. You will find it in your app lists and be able to pin it to your home screen, start menus or task bars. This installed web app will also be able to safely interact with other apps and your operating system.
If you shed tears when you miss the sun, you also miss the stars.