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;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        if(head == NULL) return NULL;
        int cnt = 0;
        ListNode *tmp = head;
        while(tmp){
            ++cnt;
            tmp = tmp -> next;
        }
        if(cnt == n){
            return head -> next;
        }
        head -> next = removeNthFromEnd(head -> next, n);
        return head;
    }
};

法二:快慢指针,快指针先比慢指针多走N步,然后两者同时走,这样的话,当快指针的next指向NULL时,说明慢指针的next指向要被删除的节点。

注意:fast先走n步后,如果为NULL,表示链表长为n,则直接删除头结点,返回head->next即可。

/**
 * 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) {
        ListNode* fast = head;
        ListNode* slow = head;
        while(n--){
            fast = fast -> next;
        }
        if(fast == NULL) return head -> next;
        while(fast -> next){
            fast = fast -> next;
            slow = slow -> next;
        }
        slow -> next = slow -> next -> next;
        return head;
    }
};

 

原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/12347201.html

时间: 2024-11-05 16:31:12

LeetCode 19. Remove Nth Node From End of List(删除链表中倒数第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

[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

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

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

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 (快慢指针)

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 the End of list

从移动回来一个月,就一直在忙项目申请书的事情.通过写申请书,看新闻联播的新闻稿都开始顺眼了. 师兄师姐们已经开始找工作了,我还有一年时间,工作方面早作准备.机器学习方面继续推导常见的算法,刷一刷kaggle,做做特征工程什么的,算是应用了. 今天这道题其实并不难,但我是在Linux下写的,有些本地调试的方法和技巧想记录一下. 一.题目 Given a linked list, remove the nth node from the end of list and return its head