19. Remove Nth Node From End of List
2016-01-26 20:47
561 查看
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.
ListNode fast = head, slow = head; if(head == null){ //链表中两个指针的思想灰常重要,一定要掌握! return null; } while(n != 0){ fast = fast.next; n--; } if(fast == null ){ element is the head //注意特殊情况!!好好想想什么时候要考虑这些边界值 return head.next; } while(fast.next != null){ slow = slow.next; fast = fast.next; } slow.next = slow.next.next; //被删的节点不用清除么?为什么=null会有错
相关文章推荐
- Hadoop namenode无法启动
- node.js REPL
- nodejs 中的 NODE_PATH
- nodejs事件
- [转]nodejs npm常用命令
- 格式化 namenode 时出现的 WARN
- Node.js 学习(六)Node.js EventEmitter
- Node做网页爬虫时遇到的Html entites对象造成乱码
- node.js安装express:'express' 不是内部或外部命令,也不是可运行的程序或批处理文件。
- Node.js 学习(五)Node.js 事件循环
- mac下配置Node.js开发环境、express安装、创建项目
- subline上装node.js插件
- Node.js 学习(四)Node.js 回调函数
- Node.js 学习(三) NPM 使用介绍
- Node.js 学习(二) 创建第一个应用
- Node.js 学习(一) 安装配置
- node.js初探
- 前端工程师使用node.js打造日记社区教程之序章
- nodeType
- 基于Node.js实现nodemailer邮件发送