【Remove Nth Node From End of List】cpp

题目

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.

代码:9ms过集合

/**
 * 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 ) return head;
        ListNode dummy(-1);
        dummy.next = head;
        ListNode *p1 = &dummy, *p2 = &dummy;
        for (size_t i = 0; i < n; ++i, p2=p2->next);
        for (; p2->next; p1=p1->next, p2=p2->next);
        ListNode *tmp = p1->next;
        p1->next = p1->next==NULL ? NULL : p1->next->next;
        delete tmp;
        return dummy.next;
    }
};

Tips:

双指针技巧:

  a. p2先走n步

  b. p1和p2一起走,直到p2走到最后一个元素

  c. 删除元素

注意再删除元素的时候,保护一下p1->next指针不为NULL。

时间: 2024-10-29 19:06:26

【Remove Nth Node From End of List】cpp的相关文章

leetcode 【 Remove Nth Node From End of List 】 python 实现

题目: 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刷题笔记】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】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: Remove Nth Node From End of List [019]

[题目] 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: G

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

Merge Two Sorted Lists &amp; Remove Nth Node From End of List

1.合并两个排好序的list Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 2.删除list倒数第n个元素 Remove Nth Node From End of List Given a linked list,

[Leetcode][Python]19: Remove Nth Node From End of List

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 19: Remove Nth Node From End of Listhttps://oj.leetcode.com/problems/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 ex

63. Swap Nodes in Pairs &amp;&amp; Rotate List &amp;&amp; Remove Nth Node From End of List

Swap Nodes in Pairs 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 should use only constant space. You may not modify the va

[lintcode easy]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. Example Given linked list: 1->2->3->4->5->null, and n = 2. After removing the second node from the end, the linked list beco