[leetcode 19] Remove Nth Node From End of List
1 题目
Given a linked list, remove the nth node from the end of list and return its head.
For example,
Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5.
Note:
Given n will always be valid.
Try to do this in one pass.
2 思路
题目要求遍历一遍搞定,那么,就可以用两个指针,先第一个先往前走N个,然后再一起走,最后,移除后一个元素。自己做出来的。。
3 代码
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode node = head;
while(n>0 && node!=null){
node = node.next;
n--;
}
ListNode start = head;
if(node != null){
while(node.next != null){
node = node.next;
start = start.next;
}
}else{
/* will remove the first object */
start = start.next;
return start;
} start.next = start.next.next;
return head;
}
[leetcode 19] Remove Nth Node From End of List的更多相关文章
- [LeetCode] 19. Remove Nth Node From End of List 移除链表倒数第N个节点
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
- Java [leetcode 19]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 19——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 19. Remove Nth Node From End of List
Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...
- [leetcode]19. Remove Nth Node From End of List删除链表倒数第N个节点
Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...
- 蜗牛慢慢爬 LeetCode 19. Remove Nth Node From End of List [Difficulty: Medium]
题目 Given a linked list, remove the nth node from the end of list and return its head. For example, G ...
- [LeetCode] 19. 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]19. Remove Nth Node From End of List删除链表的倒数第N个节点
Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...
- leetcode 19. 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 ...
随机推荐
- IP地址更改小工具(bat命令)
为了方便切换IP地址,特编制bat命令代码来实现,将以下代码复制到txt文本中,然后保存为bat文件,双击bat文件运行即可. 通过bat命令运行,自动修改IP地址,代码如下: @echo off c ...
- web服务器长连接
web服务器都提供长连接的方式,所谓长连接就是客户端一次请求完后,不关闭连接,保持一段时间的连接,下次此客户端再次请求时,不用创建新连接,复用所保持的连接即可.从理论上,长连接可以免去大量建立和关闭连 ...
- Timus Online Judge 1001. Reverse Root
Input The input stream contains a set of integer numbers Ai (0 ≤ Ai ≤ 1018). The numbers are separat ...
- 5.Makefile的原理及应用
1.概念 目标:目标顶格写,后面是冒号(冒号后面是依赖) 依赖:依赖是用来产生目标的原材料. 命令:命令前面一定是两个Tab,不能是定格,也不能说多个空格.命令就是要生成那个目标需要做的动作. 2.基 ...
- VerbalExpressions ——另类正则表达式
对于文本处理来说,正则表达式无疑是一个非常强大的工具.但是编写和阅读正则表达式往往就不是那么一件非常愉快的事情了.本文在这里介绍另一种另类的正则表达式——VerbalExpressions,它采用函数 ...
- MySQL的表的优化和列类型的选择
列选择原则: 1:字段类型优先级 整型 > date,time > enum,char>varchar > blob 列的特点分析: 整型: 定长,没有国家/地区之分,没有 ...
- make: g77: Command not found
编译cblas时报错,这时,修改Makefile.in中的编译文件中的g77为gfortran
- c#访问Oracle问题及解决方法
Q:访问oracle 查询条件带汉字结果集为空的问题 A:数据库连接字符串中加入Unicode=true即可. 如 <add key="DbConnectionString" ...
- how spring resolves a request
quoted from answer at http://stackoverflow.com/questions/14015642/how-does-the-dispatcherservlet-res ...
- 重置mysql数据库密码相关方法
方法一: 在my.ini的[mysqld]字段加入:skip-grant-tables重启mysql服务,这时的mysql不需要密码即可登录数据库 然后进入mysqlmysql>use mysq ...