【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 removing the second node from the end, the linked list becomes 1->2->3->5.

  Note:

  Given n will always be valid.

  Try to do this in one pass.

题目大意

  删除单链表的倒数第N个结点,注意:输入的N都是合法,在一次遍历中完成操作。

解题思路

  先让一个指针走找到第N个节点,然后再让一个指针指向头结点,然后两具指针一起走,直到前一个指针直到了末尾,后一个指针就是倒数第N+1个结点,删除倒数第N个结点就可以了。

代码实现

链表结点类

public class ListNode {
    int val;
    ListNode next;

    ListNode(int x) {
        val = x;
        next = null;
    }
}

算法实现类

public class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode pa = head;
        ListNode pb = head;

        // 找到第n个结点
        for (int i = 0; i < n && pa != null; i++) {
            pa = pa.next;
        }

        if (pa == null) {
            head = head.next;
            return head;
        }

        // pb与pa相差n-1个结点
        // 当pa.next为null,pb在倒数第n+1个位置
        while (pa.next != null) {
            pa = pa.next;
            pb = pb.next;
        }

        pb.next = pb.next.next;
        return head;
    }
}

评测结果

  点击图片,鼠标不释放,拖动一段位置,释放后在新的窗口中查看完整图片。

特别说明

欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/46997239

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-04 16:30:34

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

【LeetCode-面试算法经典-Java实现】【024-Swap Nodes in Pairs(成对交换单链表的结点)】

[024-Swap Nodes in Pairs(成对交换单链表的结点)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm s

【LeetCode-面试算法经典-Java实现】【083-Remove Duplicates from Sorted List(排序的单链表中删除重复的结点)】

[083-Remove Duplicates from Sorted List(排序的单链表中删除重复的结点)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a sorted linked list, delete all duplicates such that each element appear only once. For example, Given 1->1->2, return 1->2. Given 1->1->2

[LeetCode] 019. Remove Nth Node From End of List (Easy) (C++/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 019.Remove_Nth_Node_From_End_of_List (Easy) 链接: 题目:https://oj.leetcode.com/problems/remove-nth-node-from-end-of-list/ 代码(github):https://github.com/illuz/leet

【LeetCode-面试算法经典-Java实现】【109-Convert Sorted List to Binary Search Tree(排序链表转换成二叉排序树)】

[109-Convert Sorted List to Binary Search Tree(排序链表转换成二叉排序树)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 题目大意 给定一个升序的单链表.将它转换成一颗高度平衡的二叉树 解题思路 解法

LeetCode 019 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. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list bec

Java for LeetCode 019 Remove Nth Node From End of List

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

LeetCode 19 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: Gi

[Leetcode][019] Remove Nth Node From End of List (Java)

题目在这里: https://leetcode.com/problems/remove-nth-node-from-end-of-list/ [标签] Linked List; Two Pointers [个人分析] 这个题目应该算是Linked List里面的基础题.说它基础不是因为它简单,而是因为它是基石.一些 Linked list中经典方法在这道题里面都有应用. 1. Two Pointers 在 Linked List中: 如果能预先链表知道长度,那题目就简单多了,删掉从头开始的 Le

leetcode 题解 || Remove Nth Node From End of List 问题

problem: 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. Not