[Lintcode]Nth to Last Node in List
来源:互联网 发布:暴雪铁人五项 知乎 编辑:程序博客网 时间:2024/05/19 13:08
Find the nth to last element of a singly linked list.
The minimum number of nodes in list is n.
Example
Given a List 3->2->1->5->null and n = 2, return node whose value is 1.
解法:创建两个指针,两个指针保持间距为n并同时向后移动直到second指针为空。first指针为倒数第n个结点
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) { ListNode first = head; ListNode second = head; for(int i = 0; i < n; i++) { if(second == null) return null; second = second.next; } while(second != null) { first = first.next; second = second.next; } return first; }}
0 0
- [Lintcode]Nth to Last Node in List
- nth-to-last-node-in-list
- Nth to Last Node in List
- [刷题]Nth to Last Node in List
- #166 Nth to Last Node in List
- Nth to Last Node in List
- LintCode Nth to Last Node in List 链表倒数第n个节点
- LintCode | Easy | 链表倒数第n个节点 | Nth to Last Node in List
- 快慢指针-----Nth to Last Node in List
- Lintcode166 Nth to Last Node in List solution 题解
- 166.Nth to Last Node in List-链表倒数第n个节点(容易题)
- Ch2-2: return the nth to the last node data of a singly linked list
- [Lintcode]Remove Nth Node From End of List
- lintcode: Remove Nth Node From End of List
- Lintcode LinkedList 174 Remove Nth Node From End of List
- LintCode 174:Remove Nth Node From End of List
- LintCode Remove Nth Node From End of List 删除链表中倒数第n个节点
- Find the nth to last element of a singly linked list
- json字符串和json对象异同和互相转换
- 品牌人格的树立
- Struts2 pojo类中包含Date类型属性使用自定类型转换器
- *[Lintcode]Minimum Subarray
- PyCUDA 学习笔记 -- pagelocked memory
- [Lintcode]Nth to Last Node in List
- 51Nod 1163 最高的奖励(贪心+优先队列/并查集合)
- 黑客教程
- [Lintcode]Number of Islands
- 悲观的人更容易获得好的感觉
- 人工智能技术与现代应用
- *[Lintcode]O(1) Check Power of 2
- EXCEL所有单元格加减乘除一个数 -- 选择性粘贴的用法
- 细说产品经理