LeetCode:Remove Nth Node From End of List

1、题目名称

Remove Nth Node From End of List(移除链表中倒数第n项)

2、题目地址

https://leetcode.com/problems/remove-nth-node-from-end-of-list/

3、题目内容

英文:Given a linked list, remove the nth node from the end of list and return its head.

中文:给出一个链表,删去链表中的倒数第n项,返回链表首节点

例如:

给出链表:1->2->3->4->5,给出 n = 2,返回的链表为:1->2->3->5

4、解题方法1

删去链表的倒数第n项,有两种办法,一是将链表翻转,把正数第n项删去,再将链表翻转回去。

Java代码如下:

/**
 * 功能说明:LeetCode 19 - Remove Nth Node From End
 * 开发人员:Tsybius2014
 * 开发时间:2015年8月6日
 */
public class Solution {
    
    /**
     * 删除链表中倒数第N项(从1开始计数)
     * @param head 链表首节点
     * @param n 正整数
     * @return 删除后链表首节点
     */
    public ListNode removeNthFromEnd(ListNode head, int n) {
        
        if (head == null || n <= 0) {
            return head;
        }
        
        head = reverseList(head);
        head = removeNthFromBegin(head, n);
        head = reverseList(head);
        
        return head;
    }

    /**
     * 删除链表中倒数第N项(从1开始计数)
     * @param head 链表首节点
     * @param n 正整数
     * @return 删除后链表首节点
     */
    private ListNode removeNthFromBegin(ListNode head, int n) {
        
        if (head == null || n <= 0) {
            return head;
        }
        
        System.out.println("REMOVE NTH LIST NODE: " + n);
        
        if (n == 1) {
            return head.next;
        }
        
        ListNode nodeI = head;
        ListNode nodeJ = head.next;
        n--;
        while (--n != 0) {
            nodeI = nodeI.next;
            nodeJ = nodeJ.next;
            if (nodeJ == null) {
                return head;
            }
        }
        
        nodeI.next = nodeJ.next;
        return head;
    }
    
    /**
     * 翻转链表
     * @param head 翻转前链表首节点
     * @return 翻转后链表首节点
     */
    private ListNode reverseList(ListNode head) {
        
        if (head == null || head.next == null) {
            return head;
        }

        System.out.println("REVERSE LIST");
        
        ListNode nodeReverseHead = null;
        ListNode nodeTemp;
        ListNode nodeI = head;
        while (nodeI != null) {
            nodeTemp = new ListNode(nodeI.val);
            nodeTemp.next = nodeReverseHead;
            nodeReverseHead = nodeTemp;
            nodeI = nodeI.next;
        }
        
        return nodeReverseHead;
    }
}

6、解题方法2

比解题方法1更简单的方法是,先遍历一次链表获取链表长度len,第二次遍历时遍历到第len-n项时就可以进行跳过。

Java代码如下:

/**
 * 功能说明:LeetCode 19 - Remove Nth Node From End
 * 开发人员:Tsybius2014
 * 开发时间:2015年8月6日
 */
public class Solution {
    
    /**
     * 删除链表中倒数第N项(从1开始计数)
     * @param head 链表首节点
     * @param n 正整数
     * @return 删除后链表首节点
     */
    public ListNode removeNthFromEnd(ListNode head, int n) {
        
        if (head == null || n <= 0) {
            return head;
        }
    
        //获取链表总长度
        int len = 0;
        ListNode nodeTemp = head;
        while (nodeTemp != null) {
            nodeTemp = nodeTemp.next;
            len++;
        }
        
        //删去倒数第N个点
        if (len < n) {
            return head;
        } else if (len == n) {
            return head.next;
        } else {
            int counter = len - n;
            nodeTemp = head;
            while (--counter != 0) {
                nodeTemp = nodeTemp.next;
            }
            nodeTemp.next = nodeTemp.next.next;
        }
        
        return head;
    }
}

END

时间: 2024-10-14 00:59:19

LeetCode:Remove Nth Node From End of List的相关文章

LeetCode OJ - 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:Give

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

【LeetCode】Remove Nth Node From End of List (2 solutions)

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

leetCode 19. Remove Nth Node From End of List 链表

19. 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 lis

leetCode 19.Remove Nth Node From End of List(删除倒数第n个节点) 解题思路和方法

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

[LeetCode][JavaScript]Remove Nth Node From End of List

https://leetcode.com/problems/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

LeetCode之“链表”: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. N

LeetCode 19: 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】Remove Nth Node From End of List(easy)

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. 思路: 最基本的思路肯定