【数据结构】链表(leetcode)

【数据结构】链表(leetcode)

目录

① 203.移除链表元素

② 206.反转链表

③ 876.链表的中间节点 

④ 返回倒数第k个节点(面试题)

⑤ 21.合并两个有序链表

⑥ 160.相交链表 

⑦ 138.随机链表的复制(深拷贝)



① 203.移除链表元素

d489e57558fa48ac852b2823a86dffa2.png

 

/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ typedef struct ListNode ListNode; struct ListNode* removeElements(struct ListNode* head, int val) { ListNode*newHead = NULL, *newTail = NULL; ListNode*pcur = head; while(pcur != NULL) { if(pcur->val != val) { if(newHead == NULL) { //将新链表的头尾指针指向原链表头节点 newHead = newTail = pcur; } else { newTail->next = pcur; newTail = newTail->next; } } pcur = pcur->next; } if(newTail != NULL) { newTail->next = NULL; } return newHead; }

 

 


 

 

② 206.反转链表

 

39aa95d0fe0944d18f93268d49f5a543.png
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode* reverseList(struct ListNode* head) { struct ListNode* prev = NULL;//哨兵位 struct ListNode* cur = head;//头节点 while (cur) { // 哨兵位(prev) 节点1(cur) 节点2(cur->next) struct ListNode* next = cur->next;//创建一个中间节点 //开始改变链表的方向 cur->next = prev;//节点2先指向节点1的前一个节点 prev = cur;//哨兵位往后移动 cur = next;//节点1向后移动 } return prev; } 

 

 


 

 

③ 876.链表的中间节点 

f316c4e6c2dd4673b9b27bc84ad73646.png
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode* middleNode(struct ListNode* head) { //慢指针(一次走一步) struct ListNode* slow = head; //快指针(一次走两步) struct ListNode* fast = head; while(fast && fast->next){ slow = slow->next; fast = fast->next->next; } return slow; }

 

 


 

 

④ 返回倒数第k个节点(面试题)

7d8c78d2f96c4b049b90362c8144706b.png
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ int kthToLast(struct ListNode* head, int k) { struct ListNode *fast = head, *slow = head; //本题采用的是相对法: // fast先运动k个节点 // 假设该链表的节点个数是 m+k 个 // 则先走k个节点,剩下fast指针到null指针时,即走了m个节点 // 此时,slow指针就剩余k个节点 while(k--){ fast = fast->next; } while(fast != NULL){ slow = slow->next; fast = fast->next; } return slow->val; }

 

 


 

 

⑤ 21.合并两个有序链表

 

33bda6c74a77458eae7845409d991cd1.png
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) { if(list1 == NULL) { return list2; }if(list2 == NULL) { return list1; } struct ListNode* l1 = list1; struct ListNode* l2 = list2; struct ListNode* newHead, *newTail; newHead = newTail = NULL; while(l1 && l2) { //比大小 if(l1->val < l2->val) { if(newHead == NULL) { newHead = newTail = l1; } else { newTail->next = l1; newTail = newTail->next; } l1 = l1->next; } else { if(newHead == NULL) { newHead = newTail = l2; }else { newTail->next = l2; newTail = newTail->next; } l2 = l2->next; } } if(l1) { newTail->next = l1; } if(l2) { newTail->next = l2; } return newHead; }

 

 


 

 

⑥ 160.相交链表 

ec919a9804a74894a852973faae88385.png
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) { struct ListNode* curA = headA, *curB = headB; int lenA = 1, lenB = 1; while(curA->next){ curA = curA->next; ++lenA; } while(curB->next){ curB = curB->next; ++lenB; } if(curA != curB){ return NULL; } //假设法 int gap = abs(lenA - lenB); struct ListNode* longList = headA, *shortList = headB; if(lenB > lenA){ longList = headB; shortList = headA; }` while(gap--){ longList = longList->next; } while(longList != shortList){ longList = longList->next; shortList = shortList->next; } return longList; }

 

 


 

 

⑦ 138.随机链表的复制(深拷贝)

1369640e4ebc41a29064d21f90ee31cf.png
53510b52c9804686aa1fe9e98b51005d.png

 

/** * Definition for a Node. * struct Node { * int val; * struct Node *next; * struct Node *random; * }; */ 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链表插入原链表中 copy->next = cur->next; cur->next = copy; //cur向后移动 cur = copy->next; } //将copy链表中random指针的指向与原链表中的指针对调 cur = head; while(cur){ struct Node* copy = cur->next; if(cur->random == NULL){ copy->random = NULL; } else{ copy->random = cur->random->next; } cur = copy->next; } cur = head; struct Node* copyHead = NULL,* copyTail =NULL; while(cur){ struct Node* copy = cur->next; if(copyTail == NULL) { copyHead = copyTail = copy; }else { copyTail->next = copy; copyTail = copyTail->next; } //cur向后移动 cur = copy->next; } return copyHead; }

 


Read more

Python从0到100完整学习指南(必看导航)

Python从0到100完整学习指南(必看导航)

前言:零基础学Python:Python从0到100最新最全教程。 想做这件事情很久了,这次我更新了自己所写过的所有博客,汇集成了Python从0到100,共一百节课,帮助大家一个月时间里从零基础到学习Python基础语法、Python爬虫、Web开发、 计算机视觉、机器学习、神经网络以及人工智能相关知识,成为学业升学和工作就业的先行者! 【优惠信息】 • 新专栏订阅前1000名享9.9元优惠 • 订阅量破1000后价格上涨至19.9元 • 订阅本专栏可免费加入粉丝福利群,享受: - 所有问题解答 - 专属福利领取 欢迎大家订阅专栏:零基础学Python:Python从0到100最新最全教程! 本文目录: * 一、Python基础与编程入门(第1-15篇) * 1.环境搭建与语法基础 * 2.数据结构基础篇 * 3.函数编程篇 * 二、面向对象与文件处理(第16-24篇) * 1.面向对象编程篇 * 2.标准库与文件处理篇 * 三、并发编程与网络爬虫(第25-39篇) * 1.并发编程基础篇

By Ne0inhk
python环境搭建(普通python、PyCharm )

python环境搭建(普通python、PyCharm )

步骤 1:安装 PyCharm 1. 访问 JetBrains 官网:https://www.jetbrains.com/pycharm/download/Download PyCharm: The Python IDE for data science and web development by JetBrains 2. 最后点击完成即可 下载完成后,运行安装程序,按照提示完成安装 向下滚动界面 找到PyCharm Community Edition 进行下载Community 版免费 选择适合你系统的版本(Community 版免费,Professional 版功能更丰富但需付费) 步骤 2:安装 Python 解释器 如果你还没有安装 Python,

By Ne0inhk
【C++】类和对象—(下) 收官之战

【C++】类和对象—(下) 收官之战

前言:上一篇文章我们向大家介绍了类和对象的核心六个成员函数中的4个,其余两个以及初始化列表,static成员,内部类,匿名对象等会在本篇文章介绍! ✨ 坚持用清晰易懂的图解+代码语言, 让每个知识点都简单直观! 🚀 个人主页 :MSTcheng · ZEEKLOG 🌱 代码仓库 :MSTcheng · Gitee 📌 专栏系列 :📖 《C语言》🧩 《数据结构》💡 《C++由浅入深》💬 座右铭 :“路虽远行则将至,事虽难做则必成!” 文章目录 * 一,运算符重载 * 1.1什么是运算符重载? * 1.2 为什么要创造运算符重载? * 二,赋值运算符重载 * 2.1赋值运算符重载的构成 * 2.1 >>流插入<<流提取重载 * 3.1const成员函数 * 4.1取地址运算符重载 * 三,初始化列表 * 3.

By Ne0inhk