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

算法刷题之链表

// 单链表
struct ListNode {int val;  // 节点上存储的元素ListNode *next;  // 指向下一个节点的指针ListNode(int x) : val(x), next(NULL) {}  // 节点的构造函数
};ListNode* head = new ListNode(5);

重要方法:虚拟头节点 

个人方法:指针转为数组(详见4、5)

1.移除链表元素

203. 移除链表元素 - 力扣(LeetCode)
方法一:

ListNode* removeElements(ListNode* head, int val) {// 删除头结点while (head != NULL && head->val == val) { // 注意这里不是ifListNode* tmp = head;head = head->next;delete tmp;}// 删除非头结点ListNode* cur = head;while (cur != NULL && cur->next!= NULL) {if (cur->next->val == val) {ListNode* tmp = cur->next;cur->next = cur->next->next;delete tmp;} else {cur = cur->next;}}return head;
}

方法二(虚拟头节点):

ListNode* removeElements(ListNode* head, int val) {ListNode* dummyHead = new ListNode(0); // 设置一个虚拟头结点dummyHead->next = head; // 将虚拟头结点指向head,这样方便后面做删除操作ListNode* cur = dummyHead;while (cur->next != NULL) {if(cur->next->val == val) {ListNode* tmp = cur->next;cur->next = cur->next->next;delete tmp;} else {cur = cur->next;}}head = dummyHead->next;delete dummyHead;return head;
}

2.设计链表

707. 设计链表 - 力扣(LeetCode)

3.反转链表

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

ListNode* reverseList(ListNode* head) {ListNode* dummy = new ListNode();ListNode* cur;while(head != nullptr){cur = head;head = head->next;cur->next = dummy->next;dummy->next = cur;}return dummy->next;
}

4.链表相交

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

ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {// 转化为指针数组vector<ListNode*> a, b;while(headA != NULL){a.push_back(headA);headA = headA->next;}while(headB != NULL){b.push_back(headB);headB = headB->next;}int mini = min(a.size(), b.size());ListNode* res = NULL;for(int i = 1; i <= mini; i ++){if(a[a.size() - i] == b[b.size() - i]) res = a[a.size() - i];}return res;
}

 5.环形链表

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

方法一:暴力

ListNode *detectCycle(ListNode *head) {vector<ListNode*> array;ListNode* res = NULL;while(head != NULL){for(int i = 0; i < array.size(); i ++ ){if(array[i] == head) return head;}array.push_back(head);head = head->next;}return res;
}

方法二:双指针

ListNode *detectCycle(ListNode *head) {ListNode* fast = head;ListNode* slow = head;while(fast != NULL && fast->next != NULL) {slow = slow->next;fast = fast->next->next;// 快慢指针相遇,此时从head 和 相遇点,同时查找直至相遇if (slow == fast) {ListNode* index1 = fast;ListNode* index2 = head;while (index1 != index2) {index1 = index1->next;index2 = index2->next;}return index2; // 返回环的入口}}return NULL;
}
http://www.lryc.cn/news/416893.html

相关文章:

  • C# 设计模式之适配器模式
  • BFS实现迷宫最短路径
  • Linux IPC解析:匿名命名管道与共享内存
  • Codeforces Round 964 (Div. 4) A~G
  • 单体应用提高性能和处理高并发-使用缓存
  • ollama教程——使用LangChain调用Ollama接口实现ReAct
  • 【Bug分析】Keil报错:error: #18:expected a “)“问题解决
  • MAC上设置快捷打开终端以及如何运用剪切快捷键
  • linux docker安装 gitlab后忘记root密码如何找回
  • C语言典型例题27
  • clion开发stm32f4系列(一)————移植rt-thread os系统
  • 计算机网络(网络层)
  • Python3 第六十六课 -- CGI编程
  • 【Unity23种设计模式】之状态模式
  • 二叉树刷题,bfs刷题
  • 为什么要用分布式锁
  • python游戏开发之五子棋游戏制作
  • 文件上传绕过最新版安全狗
  • 常用API_2:应用程序编程接口:ArrayList
  • 【Linux操作系统】进程的基本概念(PCB对象)详解
  • 曙光宁畅中科可控所有服务器机型出厂默认IPMI用户密码
  • mysql查线上数据注意数据库的隔离级别
  • 【专业解析】移动硬盘能识别却打不开:数据恢复实战指南
  • 系统 hap
  • 【Material-UI】按钮与第三方路由库的集成详解
  • Python获取Excel内容
  • python实现小游戏随机猜数
  • YOLOv5与YOLOv8 训练准备工作(不包含环境搭建)
  • 字节跳动发Seed-TTS语音合成模型,可模仿任意人的声音,效果逼真
  • 微信小程序教程011-3:京西购物商城实战之Home页实现