力扣刷题-热题100题-第29题(c++、python)

19. 删除链表的倒数第 N 个结点 - 力扣(LeetCode)https://leetcode.cn/problems/remove-nth-node-from-end-of-list/description/?envType=study-plan-v2&envId=top-100-liked

计算链表长度

对于链表,难的就是不知道有多少元素,所以先遍历一次链表得到元素个数,然后根据要删除的位置可以在再一次遍历时找到删除的元素的前一个元素位置。需要注意的是若要求删除的是第一个元素,因为第一个元素前没有 元素,所以不好操作,因此我们手动设置一个头指针指向第一个元素。

//c++
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) 
    {
        ListNode* h=new ListNode(0,head);
        ListNode* a=h;
        int m=0;
        while(head)
        {
            m=m+1;
            head=head->next;
        }
        for(int i=0;inext;
        a->next=a->next->next;
        return h->next;
    }
};

#python
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
        m=0
        h=head
        while h:
            m=m+1
            h=h.next
        h=ListNode(0,head)
        a=h
        for i in range(m-n):
            a=a.next
        a.next=a.next.next
        return h.next

有一种数据结构是后进先出,代入这道题完美适应,所以遍历链表,使元素入栈,注意这里也要加入头指针指向第一个元素,然后根据倒数第几个元素要删除的去出栈,最后选取栈顶元素也就是要删除元素的前一个元素进行 操作。

//c++
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) 
    {
        ListNode* h=new ListNode(0,head);
        ListNode* a=h;
        stack m;
        while(a)
        {
            m.push(a);
            a=a->next;
        }
        for(int i=0;inext=b->next->next;
        return h->next;
    }
};

#python
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
        h=ListNode(0,head)
        a=h
        m=list()
        while a:
            m.append(a)
            a=a.next
        for i in range(n):
            m.pop()
        b=m[-1]
        b.next=b.next.next
        return h.next

双指针

 最喜欢这个解法,巧妙,且只需遍历一次链表,虽然两次和一次也没差。。

要加头指针!

就是两个指针,并保证这两个指针中间间隔要求的倒数n个元素,然后在快的指针到达链表尾部时,慢的指针自然而然就在要删除元素的前一个元素的位置了。

//c++
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) 
    {
        ListNode* h=new ListNode(0,head);
        ListNode* a=h;
        ListNode* b=head;
        for(int i=0;inext;
        while(b)
        {
            a=a->next;
            b=b->next;
        }
        a->next=a->next->next;
        return h->next;
    }
};

#python
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
        h=ListNode(0,head)
        a=h
        b=head
        for i in range(n):
            b=b.next
        while b:
            a=a.next
            b=b.next
        a.next=a.next.next
        return h.next

你可能感兴趣的:(leetcode,c++,算法,python)