leetcode 之Remove Nth Node From End of List(19)

这题比较简单,方法有很多。其中一种比较有意思的做法是设置两个指针,一个先走n步,然后再一起走。一个到了末尾,另一个也就确定了要删除元素的位置。
ListNode *removeNthFromEnd(ListNode *head, int n) {
ListNode dummy{-, head};
ListNode *p = &dummy, *q = &dummy;
for (int i = ; i < n; i++) // q 先走 n 步
q = q->next;
while(q->next) { // 一起走
p = p->next;
q = q->next;
}
ListNode *tmp = p->next;
p->next = p->next->next;
delete tmp;
return dummy.next;
}
leetcode 之Remove Nth Node From End of List(19)的更多相关文章
- LeetCode 019 Remove Nth Node From End of List
题目描述:Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list ...
- 【leetcode】Remove Nth Node From End of List
题目简述: Given a linked list, remove the nth node from the end of list and return its head. For example ...
- 【leetcode】Remove Nth Node From End of List(easy)
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
- 【JAVA、C++】LeetCode 019 Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
- LeetCode(46)-Remove Nth Node From End of List
题目: Given a linked list, remove the nth node from the end of list and return its head. For example, ...
- [LeetCode 题解]: Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
- leetcode 【 Remove Nth Node From End of List 】 python 实现
题目: Given a linked list, remove the nth node from the end of list and return its head. For example, ...
- leetcode 题解 || Remove Nth Node From End of List 问题
problem: Given a linked list, remove the nth node from the end of list and return its head. For exam ...
- [Leetcode][019] Remove Nth Node From End of List (Java)
题目在这里: https://leetcode.com/problems/remove-nth-node-from-end-of-list/ [标签] Linked List; Two Pointer ...
- 【LeetCode】Remove Nth Node From End of List(删除链表的倒数第N个节点)
这道题是LeetCode里的第19道题. 题目要求: 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, ...
随机推荐
- UVALive.2995 Image Is Everything (思维题 三维坐标转换)
UVALive.2995 Image Is Everything (思维题 三维坐标转换) 题意分析 这题实在是没思路,就照着打了一遍,把不理解的地方,写了注释. #include <iostr ...
- extjs gridpanel 操作行 得到选中行
extjs gridpanel 操作行 得到选中行的列 在Extjs 3.2.0上适合 var model = grid.getSelectionModel(); model.selectAll(); ...
- 搭建openresty需要注意到的地方
openresty的完整包放在百度云盘linux目录下 一键安装openresty ./install.sh 安装好后,修改nginx.conf配置文件 cd /usr/local/openresty ...
- 集成淘宝sdk
204是安全图片的问题, 请先检测以下几点: .请检测百川控制台是否已经申请初级API. .请检测百川控制台“我的产品后台”是否开通电商SDK应用.(重点检测很多用户疏忽这一点) .debug版本的可 ...
- linux和windows多线程的异同
linux多线程及线程同步和windows的多线程之间的异同 并不是所有的程序都必须采用多线程,有时候采用多线程性能还不如单线程.采用多线程的好处如下: (1)多线程之间采用相同的地址空间,共享大部分 ...
- 裸机配置C语言运行环境
C语言程序的执行需要栈的支持.部分soc未初始化栈的情况下调用C语言程序会发生错误. start.S中一共配置了看门狗,svc栈,icache. 在x210中看门狗默认关闭,svc栈默认开启,icah ...
- 使用html5的Geolocation API实现定位
公司有个需求,需要获取用户的位置,所以看了下html5的Geolocation 这个新东西,发现挺好用的. <!DOCTYPE html> <html> <body> ...
- Tomcat设置开启时自动访问某个servlet类存在的问题
<servlet> <servlet-name>****</servlet-name> <servlet-class>****</servlet- ...
- [洛谷P2375] [NOI2014]动物园
洛谷题目链接:[NOI2014]动物园 题目描述 近日,园长发现动物园中好吃懒做的动物越来越多了.例如企鹅,只会卖萌向游客要吃的.为了整治动物园的不良风气,让动物们凭自己的真才实学向游客要吃的,园长决 ...
- Java 中的方法内部类
方法内部类就是内部类定义在外部类的方法中,方法内部类只在该方法的内部可见,即只在该方法内可以使用. 一定要注意哦:由于方法内部类不能在外部类的方法以外的地方使用,因此方法内部类不能使用访问控制符和 s ...