LeetCode 第 19 题 (Remove Nth Node From End of List)

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 n will always be valid.

Try to do this in one pass.

删除一个链表的倒数第 N 个元素。这个属于链表的常规操作。通常的做法是遍历两遍链表,第一遍时确定链表的元素总数,根据这个元素总数就可以算出倒数第 N 个元素的位置了。第二遍时删除这个元素。这道题不让遍历两遍,那么只能将链表各个元素的位置存下来了,遍历一遍之后,直接去删除那个元素。

删除链表元素时要分情况讨论。如果要求删除的这个元素根本就不在链表中,那么不用删除。如果这个元素是链表的第一个元素或最后一个元素也要特殊处理。

这些都考虑了就没有什么问题了。下面是代码:

ListNode* removeNthFromEnd(ListNode* head, int n)
{
    if(n <= 0) return head;
    vector<ListNode *> array;
    ListNode * p = head;
    while(p)
    {
        array.push_back(p);
        p = p->next;
    }
    int m = array.size();
    if(n > m) return head;
    if(n == m) //如果是链表的第一个元素,要特殊处理
    {
        return head->next;
    }
    if(n == 1) //如果是链表的最后一个元素也要特殊处理
    {
        array[m - 2]->next = NULL;
        return head;
    }
    array[m - n - 1]->next = array[m - n + 1];
    return head;
}
时间: 2024-10-23 07:42:18

LeetCode 第 19 题 (Remove Nth Node From End of List)的相关文章

leetcode第19题-Remove Nth Node From End of List

本题比较简单,主要考察了单链表的创建与删除. 但是有一个问题需要着重的考虑,如何快速定位链表的倒数第n个节点.这就需要两个辅助节点,一个节点先走到正数第n个位置,然后两个辅助节点一块往后走,最后后面的节点的位置就是我们需要的倒数第n个节点. #include<stdio.h> #include<stdlib.h> struct ListNode//定义节点 { int value; struct ListNode *next; }; ListNode *removeNthFromE

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

leetcode_19题——Remove Nth Node From End of List(链表)

Remove Nth Node From End of List Total Accepted: 54129 Total Submissions: 197759My Submissions Question Solution 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,

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

题目链接 题目要求: 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

题目要求是,给你一个单向链表,和一个数字n,删除该链表倒数第n个node,其中测试案例中n能保证一定有效. 思路很简单,用两个指针,它们两个相隔为n,当后面的指针指向链表尾的时候,前面的指针指向的node的下一个node,就是要删除的那一个. 代码如下: 1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int

19:Remove Nth Node From End of List【两指针】【链表】

题目连接:https://leetcode.com/problems/remove-nth-node-from-end-of-list/ /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ /*题意:删除链表中从最后一个结点向前的第n个结点 */ /** *思路:1.两个

[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. Note:Given n