【每日刷题】Day22

【每日刷题】Day22

🥕个人主页:开敲🍉

🔥所属专栏:每日刷题🍍

🌼文章目录🌼

[1. 1669. 合并两个链表 - 力扣(LeetCode)](#1. 1669. 合并两个链表 - 力扣(LeetCode))

[2. 11. 盛最多水的容器 - 力扣(LeetCode)](#2. 11. 盛最多水的容器 - 力扣(LeetCode))

[3. 148. 排序链表 - 力扣(LeetCode)](#3. 148. 排序链表 - 力扣(LeetCode))

1. 1669. 合并两个链表 - 力扣(LeetCode)

//思路:使用两个指针定位到需要删除的数据的两端,再使用两个指针free掉中间的数据,随后再将链表连接即可

typedef struct ListNode LN;

struct ListNode* mergeInBetween(struct ListNode* list1, int a, int b, struct ListNode* list2)

{

LN* sta = list1;

LN* des = list1;

while(--a)//定位到需要删除的第一个节点的前一个节点

{

sta = sta->next;

}

while(b+1)//定位到需要删除的最后一个节点的后一个节点

{

des = des->next;

b--;

}

LN* pmove = list2;

while(pmove->next)//定位到list2的末尾

{

pmove = pmove->next;

}

LN* pdel = sta->next;

while(pdel!=des)//删除节点

{

LN* pcur = pdel->next;

free(pdel);

pdel = pcur;

}

sta->next = list2;//连接链表

pmove->next = des;

return list1;

}

2. 11. 盛最多水的容器 - 力扣(LeetCode)

//思路:双指针。分别从数组头尾向中间遍历,每一次遍历较小值为高,算出面积,记录最大的面积返回。

int maxArea(int* height, int heightSize)

{

int ans = 0;

int left = 0;

int right = heightSize-1;

while(left<right)

{

int high = height[left]<height[right]?height[left]:height[right];//较小值为高

int area = (right-left)*high;//算出面积

ans = area>ans?area:ans;记录最大面积

height[left]<height[right]?left++:right--;//将较小的那一边向中间遍历一下

}

return ans;

}

3. 148. 排序链表 - 力扣(LeetCode)

//思路:使用数组将链表中的值存储,使用qsort库函数对数组排序,再将数组中的元素逐个放回链表中,完成链表排序

typedef struct ListNode LN;

int cmp(const void* a,const void* b)

{

return *(int*)a-*(int*)b;

}

struct ListNode* sortList(struct ListNode* head)

{

int arr[50001];

LN* pmove = head;

LN* newhead = head;

int i = 0;

while(pmove)//将链表中的值存储进数组

{

arr[i++] = pmove->val;

pmove = pmove->next;

}

qsort(arr,i,sizeof(int),cmp);//按照升序将数组排序

for(int j = 0;j<i;j++)//再将数组的元素逐个存储回链表

{

newhead->val = arr[j];

newhead = newhead->next;

}

return head;

}

相关推荐
sali-tec33 分钟前
C# 基于halcon的视觉工作流-章66 四目匹配
开发语言·人工智能·数码相机·算法·计算机视觉·c#
小明说Java39 分钟前
常见排序算法的实现
数据结构·算法·排序算法
行云流水20191 小时前
编程竞赛算法选择:理解时间复杂度提升解题效率
算法
smj2302_796826523 小时前
解决leetcode第3768题.固定长度子数组中的最小逆序对数目
python·算法·leetcode
cynicme3 小时前
力扣3531——统计被覆盖的建筑
算法·leetcode
core5124 小时前
深度解析DeepSeek-R1中GRPO强化学习算法
人工智能·算法·机器学习·deepseek·grpo
mit6.8244 小时前
计数if|
算法
a伊雪4 小时前
c++ 引用参数
c++·算法
程序员Jared5 小时前
深入浅出C语言——程序环境和预处理
c语言
应茶茶5 小时前
从 C 到 C++:详解不定参数的两种实现方式(va_args 与参数包)
c语言·开发语言·c++