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

【数据结构】链表OJ题

目录

  • 面试题 02.04 分割链表
  • 剑指 Offer II 027 回文链表
  • 160 相交链表
  • 141 环形链表
  • 142 环形链表 II
  • 138 复制带随机指针的链表

面试题 02.04 分割链表

  • 定义lesshead和greaterhead链接小于和大于等于k的值
  • 分别设置哨兵位和尾节点指针
  • 最后将两表去除哨兵位再链接

在这里插入图片描述

struct ListNode* partition(struct ListNode* head, int x)
{struct ListNode* lesshead, * lesstail, * greaterhead, * greatertail;lesshead = lesstail = (struct ListNode*)malloc(sizeof(struct ListNode));lesstail->next = NULL;greaterhead = greatertail = (struct ListNode*)malloc(sizeof(struct ListNode));greatertail->next = NULL;struct ListNode* cur = head;while (cur){if (cur->val < x){lesstail->next = cur;lesstail = cur;}else{greatertail->next = cur;greatertail = cur;}cur = cur->next;}lesstail->next = greaterhead->next;greatertail->next = NULL;struct ListNode* newhead = lesshead->next;free(lesshead);free(greaterhead);return newhead;
}

 

剑指 Offer II 027 回文链表

  • 先找到链表中间节点
  • 将中间节点以后的节点从原链表截断逆置生成新链表
  • 与原链表逐节点的值相比较
    在这里插入图片描述
//回文结构
bool isPalindrome(struct ListNode* head) {if (head == NULL){return false;}//找中间节点struct ListNode* cur = head, * slow = head, * fast = head;while (fast && fast->next){slow = slow->next;fast = fast->next->next;}struct ListNode* mid = slow;//将中间节点之后的顺序反转struct ListNode* newhead = slow;cur = newhead;struct ListNode* prev = NULL;while (cur){struct ListNode* next = cur->next;cur->next = prev;prev = cur;cur = next;}newhead = prev;//遍历两组链表,检查是否每位相等struct ListNode* cur2 = newhead;struct ListNode* cur1 = head;while (cur1 && cur2){if (cur1->val != cur2->val){return false;}cur1 = cur1->next;cur2 = cur2->next;}return true;
}

 

160 相交链表

  • 根据两链表长度求出长度差k
  • 较长的链表先走k步
  • 两表再一起走,节点地址相同时返回此节点
 int ListSize(struct ListNode * head){struct ListNode * cur=head;int len=0;while(cur){len++;cur=cur->next;}return len;}
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {int len1=ListSize(headA);int len2=ListSize(headB);int k=abs(len1-len2);struct ListNode * longlist=headA;struct ListNode * shortlist=headB;if(len1<len2){longlist=headB;shortlist=headA;}struct ListNode * cur1=longlist, *cur2=shortlist;while(k--){cur1=cur1->next;}while(cur1 && cur2){if(cur1==cur2){return cur1;}cur1=cur1->next;cur2=cur2->next;}return NULL;
}

 

141 环形链表

快慢指针法,相遇则为环形链表

//环形链表,快慢指针法
bool hasCycle(struct ListNode* head) {struct ListNode* slow = head;struct ListNode* fast = head;while (fast && fast->next){fast = fast->next->next;slow = slow->next;if (slow == fast){return true;}}return false;
}

142 环形链表 II

  • 先找到入环点
  • 相遇点到入环点的距离等于链头到入环点的距离
  • 从链头和相遇点出发,相遇点即为入环点

在这里插入图片描述

//环形链表 II
//找到入环点,再判断环形链表代码块内续写
struct ListNode* detectCycle(struct ListNode* head) {struct ListNode* slow = head, * fast = head, * meet = NULL;while (fast && fast->next){//找到快慢指针相遇点fast = fast->next->next;slow = slow->next;//相遇点到入环点的距离等于链头到入环点的距离if (slow == fast){meet = slow;while (meet != head){meet = meet->next;head = head->next;}return meet;}}return NULL;
}

138 复制带随机指针的链表

  • 在原链表每个节点后复制一个节点
  • 根据原节点设置复制节点的random
    • 注意不可复制节点的同时处理random,因为random指向位置还未完成复制
  • 将处理好的复制节点链接到newhead
    在这里插入图片描述
//复制带随机指针的链表
struct Node* copyRandomList(struct Node* head) {struct Node* cur = head;//在原链表每个节点后复制一个节点while (cur){struct Node* copy = (struct Node*)malloc(sizeof(struct Node));copy->val = cur->val;copy->next = cur->next;cur->next = copy;cur = copy->next;}cur = head;struct Node* next = NULL;//根据原节点设置复制节点的randomwhile (cur){struct Node* copy = cur->next;next = copy->next;if (cur->random == NULL){copy->random = NULL;}else{copy->random = cur->random->next;}cur = next;}//将处理好的复制节点链接到newheadcur = head;struct Node* newtail = NULL, * newhead = NULL;while (cur){struct Node* copy = cur->next;next = copy->next;if (newtail == NULL){newhead = newtail = copy;}else{newtail->next = copy;newtail = copy;}cur->next = next;cur = next;}return newhead;
}
http://www.lryc.cn/news/42909.html

相关文章:

  • 冒泡 VS 插入 VS 选择——谁更胜一筹?(附排序源码)
  • [python tools] 今天看到另一个配置工具 YACS,所以做下笔记
  • Prometheus cadvisor容器监控和node-exporter节点监控
  • 机器学习|正则化|评估方法|分类模型性能评价指标|吴恩达学习笔记
  • python迭代器详解
  • 关于Docker逃逸
  • @Autowired和@Resource区别
  • 动态内存管理详细讲解
  • Python和Excel的完美结合:常用操作汇总(案例详析)
  • 卡特兰数、斯特林数基础
  • STL——mapmultimap和setmultiset
  • 2023热门抖音权重查询小程序源码
  • 153.网络安全渗透测试—[Cobalt Strike系列]—[生成hta/exe/宏后门]
  • 如何成为优秀的程序员
  • 多线程(四):线程安全
  • [ROC-RK3568-PC] [Firefly-Android] 10min带你了解Camera的使用
  • C++之模拟实现string
  • SpringBoot实战(十三)集成 Admin
  • mke2fs命令:建立ext2文件系统
  • 免费分享一个springboot+vue的办公系统
  • STM32数据搬运工DMA
  • 4、操作系统——进程间通信(2)(system V-IPC介绍)
  • 基于CentOS Stream 9平台搭建Nacos2.0.4集群以及OpenResty反向代理
  • 老杜MySQL入门基础 第二天
  • Python深度学习实战:人脸关键点(15点)检测pytorch实现
  • linux简单入门
  • 给准备面试网络工程师岗位的应届生一些建议
  • 主线程与子线程之间相互通信(HandlerThread)
  • 13基于双层优化的电动汽车日前-实时两阶段市场竞标
  • REDIS19_zipList压缩列表详解、快递列表 - QuickList、跳表 - SkipList