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步,然后两个指针同时走,知道第一个指针为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         if (head == null || n <= 0) return null;
 9
10         ListNode first = head;
11         ListNode last = head;
12
13         for (int count = 1; count <= n; count++;) {
14             first = first.next;
15         }
16
17         while(first != null) {
18             first = first.next;
19             last = last.next;
20         }
21         return last;
22     }
23 }

转载请注明出处:cnblogs.com/beiyeqingteng/

时间: 2024-10-23 07:45:46

Nth to Last Node in List的相关文章

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

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

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-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

程序猿的自我修养清单

Data Structures 1. Integer – find number of 1s – next largest smaller – smallest larger number – determine if is palindrom – itoa, atoi – add 2 numbers w/o using + or arithmetic operators – implement *, -, / using only + – find max of two numbers w/o

LintCode链表题总结

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

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