本文已参与「新人创作礼」活动,一起开启掘金创作之路
前言
本系列主要讲解链表的经典题
注:划重点!!必考~
合并两个有序链表
- 题目描述:
将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的
- 示例:
编辑
- 提示:
- 两个链表的节点数目范围是
[0, 50]-100 <= Node.val <= 100l1和l2均按 非递减顺序 排列
- 解题思路:
如果是到原链表本身上进行合并,则需要考虑的地方会比较多(实现起来很难)
这里我们选择使用尾插法
两个链表进行结点比较,较小的则将该结点进行尾插到另一个新链表上,再比较下一个
比较完一个链表后,另一个链表存在元素未比较,将该链表整体尾插
注:这里我们可以选择双指针尾插,也可以选择带哨兵卫头结点尾插
- 参考代码:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2){
//为空链表情况
if(l1==NULL)
return l2;
if(l2==NULL)
return l1;
//创建哨兵卫头结点
struct ListNode*newhead=malloc(sizeof(struct ListNode));
newhead->next=NULL;
//遍历指针
struct ListNode*cur1=l1,*cur2=l2,*cur3=newhead;
while(cur1&&cur2)
{
if(cur1->val>cur2->val)
{
cur3->next=cur2;
cur3=cur3->next;
cur2=cur2->next;
}
else
{
cur3->next=cur1;
cur3=cur3->next;
cur1=cur1->next;
}
}
//存留节点尾插
if(cur1)
{
cur3->next=cur1;
}
if(cur2)
{
cur3->next=cur2;
}
//记录哨兵后一个空间,并释放哨兵
struct ListNode*next=newhead->next;
free(newhead);
return next;
}
- 结果:
链表分割
- 题目描述:
现有一链表的头指针 ListNode* pHead,给一定值x,编写一段代码将所有小于x的结点排在其余结点之前,且不能改变原来的数据顺序,返回重新排列后的链表的头指针。
- 解题思路:
- 因为需要相对顺序不变,所以我们选择用尾插法
- 要将小的放在前面,打的放在后面,这里我们可以选择使用两个链表
- 一个链接小的结点,另一个链接大的结点,最后再将大的链表尾插到小的链表上
- 参考代码:
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};*/
class Partition {
public:
ListNode* partition(ListNode* pHead, int x) {
//创建哨兵卫头结点
struct ListNode*longhead=(struct ListNode*)malloc(sizeof(struct ListNode));
longhead->next=NULL;
struct ListNode*lesshead=(struct ListNode*)malloc(sizeof(struct ListNode));
lesshead->next=NULL;
//创建寻址指针
struct ListNode*cur1=pHead,*cur2=longhead,*cur3=lesshead;
while(cur1)
{
//根据节点数据大小决定接上的链表的位置
if(cur1->val<x)
{
cur3->next=cur1;
cur3=cur1;
}
else
{
cur2->next=cur1;
cur2=cur1;
}
//找下一个节点
cur1=cur1->next;
}
//尾节点置空,避免成环
cur2->next=NULL;
//两个链表接成一条
cur3->next=longhead->next;
//记录头结点位置
struct ListNode*next=lesshead->next;
//释放哨兵卫
free(longhead);
free(lesshead);
return next;
}
};
- 结果: