Nth to Last Node in List
来源:互联网 发布:matlab 矩阵元素赋值 编辑:程序博客网 时间:2024/05/19 12:25
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 - 1次,然后快慢指针一起走,当fast == null 的时候,说明slow的位置就是所求的结果。
代码:
ListNode nthToLast(ListNode head, int n) { // write your code here if(head == null) return null; ListNode fast = head; ListNode slow = head; int count = 1; while(count<n && fast.next != null) { fast = fast.next; count++; } while(fast.next != null){ slow = slow.next; fast = fast.next; } return slow; }
0 0
- nth-to-last-node-in-list
- Nth to Last Node in List
- [刷题]Nth to Last Node in List
- [Lintcode]Nth to Last Node in List
- #166 Nth to Last Node in List
- Nth to Last Node in List
- 快慢指针-----Nth to Last Node in List
- Lintcode166 Nth to Last Node in List solution 题解
- 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个节点(容易题)
- Ch2-2: return the nth to the last node data of a singly linked list
- Find the nth to last element of a singly linked list
- 【10】Find nth to last element of a singly linked list
- [CrackCode] 2.2 Find the nth to last element of a singly linked list
- 快慢指针- Add to List 19. Remove Nth Node From End of List
- Palindrome Number & Remove Nth Node From End of List & Sum Root to Leaf Numbers
- define a class for a linked list and write a method to delete the nth node.
- HDU-1688-Sightseeing
- 6. Your dev and test sets should come from the same distribution 你的开发集和测试集应该来自同一分布(《MACHINE LEARNING
- android中的SVG图像的各个属性意义
- 中国电科院招渗透测试工程师和逆向分析工程师
- 北航第十二届程序设计竞赛网络预赛题解
- Nth to Last Node in List
- Recover Rotated Sorted Array
- 7. How large do the dev/test sets need to be? 开发集和测试集多大合适(《MACHINE LEARNING YEARNING》翻译)
- JsonPath的使用
- linux find
- android矢量动画
- 删除所有容器
- 8. Establish a single-number evaluation metric for your team to optimize 建立一个单一数字的评估指标(MACHINE LE
- linux awk