lintcode-easy-Nth to Last Node in List Show result
2016-03-03 07:24
477 查看
Find the nth to last element of a singly linked list.
The minimum number of nodes in list is n.
Given a List 3->2->1->5->null and n = 2, return node whose value is 1.
The minimum number of nodes in list is n.
Given a List 3->2->1->5->null and n = 2, return node whose value is 1.
/** * Definition for ListNode. * public class ListNode { * int val; * ListNode next; * ListNode(int val) { * this.val = val; * this.next = null; * } * } */ public class Solution { /** * @param head: The first node of linked list. * @param n: An integer. * @return: Nth to last node of a singly linked list. */ ListNode nthToLast(ListNode head, int n) { // write your code here ListNode fast = head; for(int i = 0; i < n; i++) fast = fast.next; ListNode slow = head; while(fast != null){ fast = fast.next; slow = slow.next; } return slow; } }
相关文章推荐
- 《Node.js开发实战详解》学习笔记
- 初步使用Node连接Mysql数据库
- 24. Swap Nodes in Pairs
- [LeetCode OJ] 19. Remove Nth Node From End of List
- node.js 安装配置 npm ,express,ejs模板
- 使用Node.js+Socket.IO搭建WebSocket实时应用【转载】
- Leetcode ☞237. Delete Node in a Linked List ☆
- 24. Swap Nodes in Pairs
- Leetcode237:Delete Node in a Linked List
- NodeJS
- nodejs环境搭建express,jade,bootstrap,Augular
- unbuntu下安装nodejs express
- node.js第一弹
- LeetCode Delete Node in a Linked List
- MAC NodeJS部署,安装,运行
- NodeJS 复制大文件防治包内存的解决办法
- 如何发布Node模块到NPM社区
- NodeJS 使用Stream 方式 copy 大文件
- NodeJS 小文件复制
- nodejs实现命令行工具(三)