当前位置: 首页 > news >正文

4.1刷题(链表)

1.203. 移除链表元素 - 力扣(LeetCode)

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        // 如果头节点直接不存在
        if (head == nullptr) {
            return nullptr;
        }
        // 如果头节点存在但是要删除
        ListNode* p = head;
        while (head!=nullptr && head->val == val) {
            head = head->next;
        }
        // 如果头节点不删除
        ListNode* pre = head;
        while (pre && pre->next) {
            p = pre->next;
            while (p && p->val == val) {
                p = p->next;
                pre->next = p;
            }
            
            pre = pre->next;
        }
        return head;
    }
};
//使用带头结点的方法进行操作
class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        ListNode dummy(0,head);
        ListNode* cur=&dummy;
        while(cur->next){
            ListNode *now=cur->next;
            if (now->val==val){
                cur->next=now->next;
                delete(now);
            }else{
                cur=cur->next;
            }
        }
        return dummy.next;
    }
};

2.206. 反转链表 - 力扣(LeetCode)

//带头节点
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode dummy=ListNode(0);
        ListNode* pre=head;
        ListNode* cur=head;

        while(pre){
            cur=pre->next;
            pre->next=dummy.next;
            dummy.next=pre;
            pre=cur;
        }
        return dummy.next;
    }
};
//不带头节点
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if(head== nullptr){
            return head;
        }

        //1.initial
        ListNode*cur=head;
        ListNode*next=nullptr;
        ListNode*pre=nullptr;
        //2.开始遍历
        while(cur){
            next=cur->next;
            cur->next=pre;
            pre=cur;
            cur=next;
        }
        return pre;
    }
};

3.24. 两两交换链表中的节点 - 力扣(LeetCode)

/**
 * 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* swapPairs(ListNode* head) {
        ListNode dummy=ListNode(0,head);
        ListNode *node0=&dummy;
        ListNode *node1=head;

        while(node1 && node1->next){
            ListNode *node2=node1->next;
            ListNode *node3=node2->next;
            //交换下一个点的信息
            node0->next=node2;
            node2->next=node1;
            node1->next=node3;
            //exchange and for
            node0=node1;
            node1=node3;
        }
        return dummy.next;
    }
};

4.19. 删除链表的倒数第 N 个结点 - 力扣(LeetCode)

class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        //0.增加一个头节点
        ListNode dimmy=ListNode(0,head);
        //1.首先遍历
        ListNode*p=head;
        int count=0;
        while(p){
            count++;
            p=p->next;
        }
        //2.进行划分,找到前驱节点
        count=count-n;
        p=&dimmy;
        while(count>0){
            count--;
            p=p->next;
        }
        //3.进行删除
        //此时p为前驱节点
        ListNode *temp=p->next;
        p->next=temp->next;
        delete(temp);
        return dimmy.next;
    }
};

5.面试题 02.07. 链表相交 - 力扣(LeetCode)

class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        unordered_set<ListNode*>cnt;
        ListNode*temp=headA;
        while(temp!=nullptr){
            cnt.insert(temp);
            temp=temp->next;
        }
        temp=headB;
        while(temp!=nullptr){
            if(cnt.count(temp)>0){
                return temp;
            }
            temp=temp->next;
        }
        return nullptr;
    }
};
//特别nb的双指针法
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        ListNode *pA = headA, *pB = headB;
        while(pA!=pB){
            pA= pA==nullptr?headB:pA->next;
            pB= pB==nullptr?headA:pB->next;
        }
        return pA;
    }
};

双指针法详细解释见面试题 02.07. 链表相交 - 力扣(LeetCode)

6.141. 环形链表 - 力扣(LeetCode)

class Solution {
public:
    bool hasCycle(ListNode *head) {
        if(head==nullptr||head->next==nullptr){
            return false;
        }
        ListNode*fast=head->next;
        ListNode*slow=head;
        while(fast!=slow){
            if(fast==nullptr||fast->next==nullptr){
                return false;
            }
            fast=fast->next->next;
            slow=slow->next;
        }
        return true;
    }
};

7.142. 环形链表 II - 力扣(LeetCode)

class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        ListNode*fast=head;
        ListNode*slow=head;
        while(fast&&fast->next){
            fast=fast->next->next;
            slow=slow->next;
            if(fast==slow){
                //表明一定有环存在
                slow=head;
                while(fast!=slow){
                    fast=fast->next;
                    slow=slow->next;
                }
                return fast;
            }
        }
        return nullptr;
    }
};

相关文章:

  • 初学STM32系统时钟设置
  • Vue 组件 - Slot 内容分发
  • Windows搭建AI大模型应用开发环境以及踩过的坑
  • 软件测试(2):selenium 4.0 特点以及新特性
  • 数据库权限获取
  • MySQL基本查询
  • LeetCode[15]三数之和
  • OpenAI重磅回归开源!首发推理模型不限商用,直面DeepSeek挑战
  • 操作系统高频(六)linux内核
  • 交叉熵损失
  • leetcode25.k个一组翻转链表
  • (二十六)Dart 中泛型的使用与优势
  • WEB安全--SQL注入--无列名注入
  • 本地合并多个仓库,保留Commit历史
  • MyBatis choose when otherwise
  • 算法设计学习2
  • 【FreeRtos】任务调度器可以被挂起吗?
  • 【配电网】基于差分进化算法的含DG配电网无功优化模型
  • python技巧:自动控制高低温箱,通过串口输入命令,生成16进制字符串,并计算CRC16。
  • 4.1-3 模拟器
  • 多家外资看好中国市场!野村建议“战术超配”,花旗上调恒指目标价
  • “水运江苏”“航运浙江”,江浙两省为何都在发力内河航运?
  • 为惩戒“工贼”,美国编剧工会“痛下杀手”
  • 哈佛新论文揭示 Transformer 模型与人脑“同步纠结”全过程!AI也会犹豫、反悔?
  • 国务院新闻办公室发布《新时代的中国国家安全》白皮书
  • 石家庄推动城市能级与民生福祉并进