zoukankan      html  css  js  c++  java
  • 代码题(67)— 删除链表的倒数第 N 个结点、链表中倒数第 K个节点、移除链表元素

    1、19. 删除链表的倒数第 N 个结点

    给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。

     双指针的经典应用,如果要删除倒数第n个节点,让fast移动n步,然后让fast和slow同时移动,直到fast指向链表末尾。删掉slow所指向的节点就可以了。

    /**
     * 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) {
            if(head == nullptr || n<=0)
                return nullptr;
            ListNode* dummy = new ListNode(-1);
            dummy->next = head;
            ListNode* fast = dummy;
            ListNode* slow = dummy;
            for(int i=0;i < n+1 ;++i){
                if(fast == nullptr){
                    return nullptr;
                }
                fast = fast->next;
            }
            while(fast) {
                fast = fast->next;
                slow = slow->next;
            }
            slow->next = slow->next->next;
            return dummy->next;
        }
    };

    2、链表中倒数第k个结点

    输入一个链表,输出该链表中倒数第k个结点。
    如果该链表长度小于k,请返回空。
       ListNode* FindKthToTail(ListNode* head, int k) {
            if(head == nullptr || k <= 0){
                return nullptr;
            }
            ListNode* fast = head, * slow = head;
            for(int i=0;i<k;++i){
                if(fast == nullptr)
                    return nullptr;
                fast = fast->next;
            }
            while(fast){
                fast = fast->next;
                slow = slow->next;
            }
            return slow;
        }

    3、203. 移除链表元素

    给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点 。

      

    /**
     * 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* removeElements(ListNode* head, int val) {
            if(head == nullptr)
                return nullptr;
            ListNode* dummy = new ListNode(-1);
            dummy->next = head;
            ListNode* pre = dummy;
            ListNode* cur = pre->next;
            while(cur){
                if(cur->val == val){
                    cur = cur->next;
                    pre->next = cur;
                } else {
                    pre = cur;
                    cur = cur->next;
                }
            }
            return dummy->next;
        }
    };
  • 相关阅读:
    iTestin云测试工具
    android 存储操作 大小显示换算 kb mb KB MB 读取
    android 发送短信 判断号码规则 判断字符数70
    android 震动 各种
    10.13总结
    10.8每日总结
    10.9
    10.15
    10.14
    10.12每日总结
  • 原文地址:https://www.cnblogs.com/eilearn/p/14806800.html
Copyright © 2011-2022 走看看