[LeetCode]78. 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 n will always be valid.
Try to do this in one pass.

Subscribe to see which companies asked this question

解法1:首先扫描一遍链表,统计共有多少个元素;然后从头开始遍历链表,直到到达要删除节点的前一个节点。因为题目限定了输入n总是有效的,所以不需要考虑n大于链表长度或者n小于等于0这些情况。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        if(head == NULL) return NULL;
        ListNode* p = head;
        int num = 0;
        while(p != NULL) {
            ++num;
            p = p->next;
        }
        if(num - n == 0) { // 删除的是头节点
            ListNode* del = head;
            head = head->next;
            delete del;
        }
        else {
            p = head;
            for(int i = 0; i < num - n - 1; ++i)
                p = p->next;
            ListNode* del = p->next;
            p->next = p->next->next;
            delete del;
        }
        return head;
    }
};

解法2:一趟遍历解决问题。因为删除某个节点需要改动其前一个节点的next指针,因此我们先找到要删除节点的前一个节点。设置快慢两个指针,初始时都指向头节点。在快指针向前移动N步之后(注意,若此时快指针到达了链表的尾部,即为NULL,则说明删除的是头节点,需要单独考虑),两个指针同时向后移动,直到快指针是尾节点。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        if(head == NULL) return NULL;
        ListNode* p1 = head;
        ListNode* p2 = head;
        for(int i = 0; i < n; ++i)
            p1 = p1->next;
        if(p1 == NULL) { //删除的是头节点
            ListNode* del = head;
            head = head->next;
            delete del;
            return head;
        }
        while(p1->next != NULL) {
            p1 = p1->next;
            p2 = p2->next;
        }
        ListNode* del = p2->next;
        p2->next = p2->next->next;
        delete del;
        return head;
    }
};
时间: 2024-12-19 09:04:24

[LeetCode]78. Remove Nth Node From end of List删除链表中倒数第N个节点的相关文章

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

[LeetCode]19. Remove Nth Node From End of List删除链表的倒数第N个节点

Given a linked list, remove the n-th node from the end of list and return its head. 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 w

19. Remove Nth Node From End of List(删除链表中的第n个结点)

Given a linked list, remove the n-th node from the end of list and return its head. 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 w

LeetCode Remove Nth Node From End of List 删除链表的倒数第n个结点

1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode *removeNthFromEnd(ListNode *head, int n) { 12 struct

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

LeetCode 19. Remove Nth Node From End of List(删除链表中倒数第N个节点)

题意:删除链表中倒数第N个节点. 法一:递归.每次统计当前链表长度,如果等于N,则return head -> next,即删除倒数第N个节点:否则的话,问题转化为子问题“对head->next这个链表删除倒数第N个节点”,将head的next指针指向该子问题的结果,返回head即可.这个方法时间复杂度高,不推荐. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next;

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项,返回链表首节点 例如:

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