[LeedCode OJ]#19 Remove Nth Node From End of List
来源:互联网 发布:大非农数据利空金银 编辑:程序博客网 时间:2024/05/18 18:46
【 声明:版权所有,转载请标明出处,请勿用于商业用途。 联系信箱:libin493073668@sina.com】
题目链接:https://leetcode.com/problems/remove-nth-node-from-end-of-list/
题意:
给出一个链表,要求删除倒数第n个节点
思路:
还是使用双指针法,让第一个指针领先第二个指针n-1的距离,当第一个指针到达了链表尾部的时候,那么第二个指针就是指向的倒数第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 *first = head;ListNode *second = head;ListNode *pre = nullptr;for(int i = 1; i<n; i++)first = first->next;while(first->next){pre = second;first = first->next;second = second->next;}if(pre==nullptr)head = second->next;elsepre->next = second->next;return head;}};
0 0
- [LeedCode OJ]#19 Remove Nth Node From End of List
- OJ 19 Remove Nth Node From End of List
- LeetCode OJ:Remove Nth Node From End of List
- LeetCode OJ - Remove Nth Node From End of List
- [LeetCode OJ]Remove Nth Node From End of List
- LeetCode OJ Remove Nth Node From End of List
- 【LeetCode OJ 019】Remove Nth Node From End of List
- leetcode oj java Remove Nth Node From End of List
- <LeetCode OJ> 19. Remove Nth Node From End of List
- [LeetCode OJ] 19. Remove Nth Node From End of List
- LeetCode OJ - Remove Nth Node From End of List
- Leetcode OJ -Remove Nth Node From End of List
- LeetCode OJ-19.Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node from End of List
- javascript 构造函数内return 解析
- 制作word生成器感悟
- 第2周项目1 函数参数传递的三种方式
- POJ3650解题报告
- 提交用户名和密码到指定的服务器
- [LeedCode OJ]#19 Remove Nth Node From End of List
- Mms 短信设置里添加常用短信时出现错误的解决办法
- 06-查看dns
- 机房收费——“上下机”还原生活
- netfilter/hook关于ICMP协议过滤的样例
- ListView+RecyclerView
- Java面试题
- sublime 常用正则表达式
- Android基础学习之Activity