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、代码:

  

 ListNode nthToLast(ListNode head, int n) {
            if (head == null || n < 1) {
                return null;
            }
        // 3->2->1->5->7->9->9->null,n=2,  1
            /**
             * 1、7大小,最后两位
             * 2、先已两位,然后停止
             * 3、一起再移余下的位数,那么原来的就变成倒数2为
             */
            ListNode p1 = head;
            ListNode p2 = head;
            for (int j = 0; j < n - 1; ++j) {
                if (p2 == null) {
                    return null;
                }
                p2 = p2.next;
            }
            while (p2.next != null) {
                p1 = p1.next;
                p2 = p2.next;
            }
            return p1;
        }
时间: 2024-10-29 10:29:47

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

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 容易题: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 Highest Salary 第N高薪水

Write a SQL query to get the nth highest salary from the Employee table. +----+--------+ | Id | Salary | +----+--------+ | 1 | 100 | | 2 | 200 | | 3 | 300 | +----+--------+ For example, given the above Employee table, the nth highest salary where n =

[LeetCode] Nth Digit 第N位

Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... Note: n is positive and will fit within the range of a 32-bit signed integer (n < 231). Example 1: Input: 3 Output: 3 Example 2: Input: 11 Output: 0 Explanatio

【一天一道LeetCode】#237. Delete Node in a Linked List

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -> 3 -> 4

LeetCode 1019. Next Greater Node In Linked List (链表中的下一个更大节点)

题目标签:Linked List, Stack 题目给了我们一个 Linked List,让我们找出对于每一个数字,它的下一个更大的数字. 首先把 Linked List 里的数字 存入 ArrayList, 方便后面的操作. 然后遍历 ArrayList,首先每一个数字,都会存入stack:所以就可以利用stack回到之前的数字,存入它的 next Greater Node. Java Solution: Runtime:  39 ms, faster than 65 % Memory Usa

[LeetCode] 237. Delete Node in a Linked Lis解题小结

题目: Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 -

【LeetCode】237 - Delete Node in a Linked List

Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 ->