LeetCode 19 Remove Nth Node From End of List(从列表尾部删除第N个结点)(*)

翻译

给定一个链表,移除从尾部起的第n个结点,并且返回它的头结点。

例如,给定链表:1->2->3->4->5,n = 2。

在移除尾部起第二个结点后,链表将变成:1->2->3->5。

备注:

给定的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.

代码

class Solution{
public:
    ListNode* removeNthFromEnd(ListNode* head, int n){
        ListNode newHead(0);
        newHead.next = head;
        count = n;
        return solution1(&newHead);
    }
private:
    int count;
    ListNode* solution1(ListNode* newHead){
        subSol1(newHead);
        return newHead->next;
    }
    bool subSol1(ListNode* node){
        if(!node) return false;
        if(subSol1(node->next)) return true;
        if(count--) return false;
        ListNode *tmp = node->next;
        node->next = tmp->next;
        delete tmp;
        return true;
    }
};
时间: 2024-10-21 09:17:11

LeetCode 19 Remove Nth Node From End of List(从列表尾部删除第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: G

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]82.Remove Duplicates from Sorted List II

[题目] Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example, Given 1->2->3->3->4->4->5, return 1->2->5. Given 1->1->1->2->3, return 2-

[LeetCode]203.Remove Linked List Elements

题目 Remove all elements from a linked list of integers that have value val. Example Given: 1 –> 2 –> 6 –> 3 –> 4 –> 5 –> 6, val = 6 Return: 1 –> 2 –> 3 –> 4 –> 5 思路 链表节点的删除操作. 代码 /*--------------------------------------- * 日期:

[LeetCode]27.Remove Element

[题目] Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new length. [题意] 把数组中与给定值相同的元素删除,在原数组上修改,返回值是最终元素个数. [分析] 无 [代码]

[LeetCode]80.Remove Duplicates from Sorted Array II

[题目] Remove Duplicates from Sorted Array II  Total Accepted: 4460 Total Submissions: 15040My Submissions Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array A = [1,1,1,2,2,3], Your fun

LeetCode 27 Remove Element(移除元素)

翻译 给定一个数组和一个值,删除该值的所有实例,并返回新的长度. 元素的顺序可以被改变,也不关心最终的数组长度. 原文 Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new lengt

LeetCode 26 Remove Duplicates from Sorted Array(从已排序数组中移除重复元素)

翻译 给定一个已排序的数组,删除重复的元素,这样每个元素只出现一次,并且返回新的数组长度. 不允许为另一个数组使用额外的空间,你必须就地以常量空间执行这个操作. 例如, 给定输入数组为 [1,1,2] 你的函数应该返回length = 2, 其前两个元素分别是1和2.它不关心你离开后的新长度. 原文 Given a sorted array, remove the duplicates in place such that each element appear only once and re

list Remove(Children.Node)

问题描述 classNode{publicstringParent{get;privateset;}publicintNo{get;privateset;}publicList<Node>Children{get;privateset;}publicNode(stringparent,intno){this.Parent=parent;this.No=no;this.Children=newList<Node>();}}privateList<Node>data=new