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;
 *     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) {
        // write your code here
        ListNode fast = head;
        for(int i = 0; i < n; i++)
            fast = fast.next;

        ListNode slow = head;

        while(fast != null){
            fast = fast.next;
            slow = slow.next;
        }

        return slow;
    }
}
时间: 2024-10-10 17:24:00

lintcode-easy-Nth to Last Node in List Show result的相关文章

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

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

[lintcode easy]Remove Nth Node From End of List

Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list and return its head. Example Given linked list: 1->2->3->4->5->null, and n = 2. After removing the second node from the end, the linked list beco

[LintCode] Remove Nth Node From End of List

Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list and return its head. Example Given linked list: 1->2->3->4->5->null, and n = 2. After removing the second node from the end, the linked list beco

[lintcode easy]Insert Node in a Binary Search Tree

Insert Node in a Binary Search Tree Example Given binary search tree as follow, after Insert node 6, the tree should be: 2 2 / \ / 1 4 --> 1 4 / / \ 3 3 6 Challenge Can you do it without recursion? /** * Definition of TreeNode: * public class TreeNod

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]Longest Words

Longest Words Given a dictionary, find all of the longest words in the dictionary. Example Given { "dog", "google", "facebook", "internationalization", "blabla" } the longest words are(is) ["internati

lintcode 容易题:Insert Node in a Binary Search Tree 在二叉查找树中插入节点

题目:  在二叉查找树中插入节点 给定一棵二叉查找树和一个新的树节点,将节点插入到树中. 你需要保证该树仍然是一棵二叉查找树.  样例 给出如下一棵二叉查找树,在插入节点6之后这棵二叉查找树可以是这样的: 挑战 能否不使用递归? 解题: 递归的方法比较简单 Java程序: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public