[leetcode 19] Remove Nth Node From End of List
2015-09-02 19:29
639 查看
1 题目
Given a linked list, remove the nth node from the end of list and return its head.
For example,
Note:
Given n will always be valid.
Try to do this in one pass.
2 思路
题目要求遍历一遍搞定,那么,就可以用两个指针,先第一个先往前走N个,然后再一起走,最后,移除后一个元素。自己做出来的。。
3 代码
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; }
相关文章推荐
- Node.js最新Web技术栈(2015年5月)
- NodeJS模块、包、NPM
- Nodejs express中创建ejs项目
- NodeJS介绍
- nodejs && apidoc
- hadoop中两个namenode都是standby
- LeetCode_Reverse Nodes in k-Group
- Node.js开发入门—Express安装与使用
- hdfs namenode -format 报错
- Nodejs OS 模块
- Ubuntu Server 14.04.3 LTS 编译安装 NodeJS
- [leetcode 222]Count Complete Tree Nodes
- hadoop-2.6.0+zookeper-3.4.6之HA模式两个namenode不同状态active和standby的切换
- node.js 针对不同的请求路径(url) 做出不同的响应
- LeetCode之 Delete Node in a Linked List
- Node.js v0.10.31API手工-DNS
- [Leetcode]#237 Delete Node in a Linked List
- [Leetcode]#24 Swap Nodes in Pairs
- [Leetcode]#19 Remove Nth Node From End of List
- CCFontAtlas,CCGrid,CCGLBufferedNode,CCGrabber解析