LintCode Nth to Last Node in List 链表倒数第n个节点
来源:互联网 发布:linux中rpm命令 编辑:程序博客网 时间:2024/05/22 07:43
找到单链表倒数第n个节点,保证链表中节点的最少数量为n。
样例
给出链表 3->2->1->5->null和n = 2,返回倒数第二个节点1.
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.
/** * 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) { ListNode p = head, q = head; while(n > 0 && p != null) { p = p.next; n--; } while(p != null) { q = q.next; p = p.next; } return q; }}
0 0
- LintCode Nth to Last Node in List 链表倒数第n个节点
- LintCode | Easy | 链表倒数第n个节点 | Nth to Last Node in List
- 166.Nth to Last Node in List-链表倒数第n个节点(容易题)
- LintCode Remove Nth Node From End of List 删除链表中倒数第n个节点
- [Lintcode]Nth to Last Node in List
- LeetCode(Remove Nth Node From End of List)删除链表倒数第n个节点
- Remove Nth Node From End of List 删除链表倒数第N个节点
- leetcode:Remove Nth Node From End of List(删除链表倒数第n个节点)【面试算法题】
- [LeetCode]—Remove Nth Node From End of List 删除链表的倒数第n个节点
- #19 Remove Nth Node From End of List(删除链表倒数第N个节点)
- leetcode 3. 移除链表的倒数第n个节点 Remove Nth Node From End of List
- 【Leetcode】19. Remove Nth Node From End of List - 删掉链表倒数第n个节点
- 19. Remove Nth Node From End of List (移除链表倒数第n个节点)
- leetcode-19. Remove Nth Node From End of List(删除链表倒数第N个节点)
- 19. Remove Nth Node From End of List(移除链表的倒数第n个节点)
- LeetCode--Remove Nth Node From End of List(移除链表的倒数第N个节点)Python
- LeetCode 19 Remove Nth Node From End of List 移除倒数第N个节点
- [LeetCode-19] Remove Nth Node From End of List(删除倒数第N个节点)
- oracle的SCN
- 理解Hibernate配置文件
- CVX: Matlab Software for Disciplined Convex Programming
- Server 2008 R2安装需求
- Panel与pane
- LintCode Nth to Last Node in List 链表倒数第n个节点
- 定位判断是否在中国
- 判断输入是数字还是字符串
- 图论中最小生成树构造算法之Prim算法和Kruskal算法
- 屏蔽控制台应用程序的窗口
- super & this
- ReactiveCocoa入门教程——第二部分
- linux tar 打包排除某些文件、文件夹
- 日语学习之沪江N3基础 20150625 -5