C/C++刷题中的指针三剑客之“链表“

编程入门 行业动态 更新时间:2024-10-11 09:28:00

C/C++刷题中的<a href=https://www.elefans.com/category/jswz/34/1768268.html style=指针三剑客之“链表“"/>

C/C++刷题中的指针三剑客之“链表“

链表概述

什么是链表,链表是一种通过指针串联在一起的线性结构,每一个节点是又两部分组成,一个是数据域一个是指针域(存放指向下一个节点的指针),最后一个节点的指针域指向null(空指针的意思)。

如图所示:

同时,链表又分为单链表,双链表,循环链表等。

下面给出链表结点的定义:

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

203.移除链表元素

题目描述

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

输入输出样例

输入:head = [1,2,6,3,4,5,6], val = 6
输出:[1,2,3,4,5]

代码

/*** 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) {ListNode* prehead=new ListNode(0,head);ListNode* pre=prehead;ListNode* cur=head;while(cur!=nullptr){if(cur->val==val){ListNode *tmp=cur;pre->next=cur->next;cur=cur->next;delete tmp;}else{pre=cur;cur=cur->next;}}head=prehead->next;delete prehead;return head;}
};

206.反转链表

题目描述

给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。

输入输出样例

输入:head = [1,2,3,4,5]
输出:[5,4,3,2,1]

代码

/*** 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* reverseList(ListNode* head) {if(head==nullptr||head->next==nullptr) return head;ListNode* pre=head;ListNode* cur=head->next;pre->next=nullptr;while(cur!=nullptr){ListNode* nex=cur->next;cur->next=pre;pre=cur;cur=nex;}return pre;}
};

24.两两交换链表中的节点

题目描述

给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。

你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。

输入输出样例

输入:head = [1,2,3,4]
输出:[2,1,4,3]

代码

/*** 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* prehead=new ListNode(0,head);ListNode* cur=prehead;while(cur->next!=nullptr&&cur->next->next!=nullptr){ListNode* next1=cur->next;ListNode* next2=cur->next->next;cur->next=next2;next1->next=next2->next;next2->next=next1;cur=cur->next->next;}return prehead->next;}
};

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

题目描述

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

输入输出样例

输入:head = [1,2,3,4,5], n = 2
输出:[1,2,3,5]

代码

/*** 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) {ListNode* prehead=new ListNode(0,head);ListNode* fast=head;ListNode* low=prehead;for(int i=0;i<n;i++){fast=fast->next;}while(fast!=nullptr){fast=fast->next;low=low->next;}low->next=low->next->next;return prehead->next;}
};

面试题02.07 链表相交

题目描述

给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点,返回 null 。

输入输出样例

输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
输出:Intersected at '8'
解释:相交节点的值为 8 (注意,如果两个链表相交则不能为 0)。
从各自的表头开始算起,链表 A 为 [4,1,8,4,5],链表 B 为 [5,0,1,8,4,5]。
在 A 中,相交节点前有 2 个节点;在 B 中,相交节点前有 3 个节点。

代码

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode(int x) : val(x), next(NULL) {}* };*/
class Solution {
public:ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {ListNode* L1=headA;ListNode* L2=headB;while(L1!=L2){if(L1!=nullptr) L1=L1->next;else L1=headB;if(L2!=nullptr) L2=L2->next;else L2=headA;}return L1;}
};

142.环形链表Ⅱ

题目描述

给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。

为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。注意,pos 仅仅是用于标识环的情况,并不会作为参数传递到函数中。

说明:不允许修改给定的链表。

输入输出样例

输入:head = [3,2,0,-4], pos = 1
输出:返回索引为 1 的链表节点
解释:链表中有一个环,其尾部连接到第二个节点。

代码

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode(int x) : val(x), next(NULL) {}* };*/
class Solution {
public:ListNode *detectCycle(ListNode *head) {ListNode* fast=head;ListNode* low=head;while(true){if(fast==nullptr||fast->next==nullptr) return nullptr;fast=fast->next->next;low=low->next;if(low==fast) break;}fast=head;while(fast!=low){fast=fast->next;low=low->next;}return fast;}
};

21.合并两个链表

题目描述

将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。

输入输出样例

输入:l1 = [1,2,4], l2 = [1,3,4]
输出:[1,1,2,3,4,4]

代码

/*** 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* mergeTwoLists(ListNode* l1, ListNode* l2) {ListNode* prehead=new ListNode(0);ListNode* ret=prehead;while(l1!=nullptr&&l2!=nullptr){if(l1->val>=l2->val){prehead->next=l2;l2=l2->next;prehead=prehead->next;}else{prehead->next=l1;l1=l1->next;prehead=prehead->next;}}if(l1==nullptr){prehead->next=l2;}else{prehead->next=l1;}return ret->next;}
};

参考资料:代码随想录

更多推荐

C/C++刷题中的指针三剑客之“链表“

本文发布于:2023-07-28 19:13:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1284051.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针   三剑客   链表   刷题中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!