Lintcode: Nth to Last Node in List

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.

Runner Technique: 两个指针都从Dummy node出发,结束条件是runner.next!=null

 1 public class Solution {
 2     /**
 3      * @param head: The first node of linked list.
 4      * @param n: An integer.
 5      * @return: Nth to last node of a singly linked list.
 6      */
 7     ListNode nthToLast(ListNode head, int n) {
 8         // write your code here
 9         ListNode dummy = new ListNode(0);
10         dummy.next = head;
11         ListNode walker = dummy;
12         ListNode runner = dummy;
13         while (runner.next != null && n>0) {
14             runner = runner.next;
15             n--;
16         }
17         while (runner.next != null) {
18             runner = runner.next;
19             walker = walker.next;
20         }
21         return walker.next;
22     }
23 }
时间: 2024-10-07 05:29:54

Lintcode: Nth to Last Node in List的相关文章

lintcode 容易题:nth to Last Node In List 链表倒数第n个节点

题目: 链表倒数第n个节点 找到单链表倒数第n个节点,保证链表中节点的最少数量为n. 样例 给出链表 3->2->1->5->null和n = 2,返回倒数第二个节点的值1. 解题: 某年408计算机考研题目 Java程序: /** * Definition for ListNode. * public class ListNode { * int val; * ListNode next; * ListNode(int val) { * this.val = val; * thi

Nth to Last Node in List

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. 分析: 要找到nth to last element,我们需要两个指针,第一个指针先走n步,然后两个指针同时走,知道第一个指针为nu

leetcode:Nth to Last Node in List

1. 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. 2. 1.由于链表没有得到长度的值,只能通过一个一个移来进行判断 2.先移n位,原来的再移length-n位既可以 3.代码: ListNod

【Lintcode】087.Remove Node in Binary Search Tree

题目: Given a root of Binary Search Tree with unique value for each node. Remove the node with given value. If there is no such a node with given value in the binary search tree, do nothing. You should keep the tree still a binary search tree after rem

LintCode链表题总结

由于链表本身结构的单一性,链表的题目很少会有很大的变种,基本都是围绕几个基本的考点出题目.所以链表的题目比较好掌握,但是链表的题目又不太容易一次就AC通过,由于边界情况未考虑.空指针(比如head.next不存在但是却给head.next赋值了,就会抛出nullpointer的错误).越界等边界情况,我们需要在测试用例的时候多考虑边界条件.在模拟计算的时候一定要用纸和笔把中间的操作过程给画出来,这样比较容易形成思路. 在LintCode的ladder1中,链表那一章有如下这一些题目: 此外,Li

lintcode-easy-Nth to Last Node in List Show result

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. /** * Definition for ListNode. * public class ListNode { * int val; * List

LintCode 链表倒数第n个节点

找到单链表倒数第n个节点,保证链表中节点的最少数量为n. 样例 给出链表 3->2->1->5->null和n = 2,返回倒数第二个节点的值1. 分析:设两个指针 p1和p2,p1遍历到n-1的位置,p2从头开始遍历 当p1到链表尾部的时候,p2刚好到倒数n的位置 注意鲁棒性的考虑 /** * Definition of ListNode * class ListNode { * public: * int val; * ListNode *next; * ListNode(in

LintCode 166. 链表倒数第n个节点

找到单链表倒数第n个节点,保证链表中节点的最少数量为n. 样例 给出链表 3->2->1->5->null和n = 2,返回倒数第二个节点的值1. /** * Definition of ListNode * class ListNode { * public: * int val; * ListNode *next; * ListNode(int val) { * this->val = val; * this->next = NULL; * } * } */ cla

166 链表倒数第n个结点

原题网址:https://www.lintcode.com/problem/nth-to-last-node-in-list/description 描述 找到单链表倒数第n个节点,保证链表中节点的最少数量为n. 您在真实的面试中是否遇到过这个题?  是 样例 给出链表 3->2->1->5->null和n = 2,返回倒数第二个节点的值1. 标签 链表 Cracking The Coding Interview 思路:首先应该清楚,若链表一共有t个结点,则正数第n个结点即倒数第t