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;
 *         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
        if(head ==null)
            return null;
        if( head.next ==null && n==1)
            return head;
        ListNode p = head;
        ListNode current = new ListNode(0);
        current.next = head;
        while( p.next!=null){
            if(n>1){
                p = p.next;
                n --;
            }else{
                p = p.next;
                current = current.next;

            }
        }
        return current.next;
    }
}

总耗时: 2548 ms

Python程序:

时间: 2024-12-27 22:55:56

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

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

[Leetcode] remove nth node from the end of list 删除链表倒数第n各节点

Given a linked list, remove the n th node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note:  Give

[LeetCode] Remove Nth Node From End of List 溢出链表倒数第N个节点

Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note: Given

lintcode 中等题:Palindrome Linked List 回文链表

题目 回文链表 设计一种方式检查一个链表是否为回文链表. 样例 1->2->1 就是一个回文链表. 挑战 O(n)的时间和O(1)的额外空间. 解题 法一: 再定义一个链表,存放链表反转的值,再以此比较两个链表中的值是否相等,时间复杂度O(N),空间复杂度O(N) /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { v

lintcode 容易题:Remove Nth Node From End of Lis 删除链表中倒数第n个节点

题目: 删除链表中倒数第n个节点 给定一个链表,删除链表中倒数第n个节点,返回链表的头节点.  样例 给出链表1->2->3->4->5->null和 n = 2. 删除倒数第二个节点之后,这个链表将变成1->2->3->5->null. 注意 链表中的节点个数大于等于n 解题: 要删除倒数第n个节点,我们要找到其前面一个节点,也就是倒数第n+1的节点,找到这个节点就可以进行删除.和上题的思想很类似, 定义两个指针,p和cur,cur指针向前走,走了n

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

【LeetCode-面试算法经典-Java实现】【019-Remove Nth Node From End of List(移除单链表的倒数第N个节点)】

[019-Remove Nth Node From End of List(移除单链表的倒数第N个节点)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After remo

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