当前位置:网站首页>LeetCode个人题解(剑指offer 21-25)21. 调整数组顺序使奇数位于偶数前面,22. 链表中倒数第k个节点,24. 反转链表,25. 合并两个排序的链表
LeetCode个人题解(剑指offer 21-25)21. 调整数组顺序使奇数位于偶数前面,22. 链表中倒数第k个节点,24. 反转链表,25. 合并两个排序的链表
2022-06-21 18:11:00 【木白CPP】
剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
题解:交换
有点采用快速排序的思想。思路很简单,从左向右,直到遇到一个偶数,从右向左,直到遇到一个奇数,两者交换即可
代码:
class Solution {
public:
vector<int> exchange(vector<int>& nums) {
int left=0,right=nums.size()-1;
while(left<right){
while(left<right&&nums[left]%2==1) ++left;
while(left<right&&nums[right]%2==0) --right;
swap(nums[left],nums[right]);
}
return nums;
}
};结果:

剑指 Offer 22. 链表中倒数第k个节点
题解:
我们都知道链表是不能随机存储的,我们想要知道倒数第k个点是多少,就需要遍历一遍链表,把链表的长度length记下来,第二遍遍历到length-k处。但是这样时间复杂度接近O(n^2)。能不能一遍就能找到?
首先,定义两个指针,一左一右,让right先走k-1个节点,left在头节点处。
然后,right一直走到链表末尾,left便走到了倒数第k个节点处,返回left即可。
代码:
class Solution {
public:
ListNode* getKthFromEnd(ListNode* head, int k) {
ListNode *left,*right;
left=right=head;
while(--k)
right=right->next;
while(right->next){
right=right->next;
left=left->next;
}
return left;
}
};结果:

剑指 Offer 24. 反转链表
定义一个函数,输入一个链表的头节点,反转该链表并输出反转后链表的头节点。
题解:
方法一:头插法
首先,排除链表为空或者只有一个数的情况。
将尾指针r走到链表最后一个节点处,pre和p两个指针从头节点开始遍历,p是pre的后继节点。
每走一个节点,便将该节点用头插法的方式插入到尾指针r后面。
该算法要遍历两次,所以时间复杂度为O(n^2)。
代码:
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(head==nullptr||head->next==nullptr) return head;
ListNode *r,*pre,*p;
pre=r=head;
p=head->next;
while(r->next)
r=r->next;
while(pre!=r){
pre->next=r->next;
r->next=pre;
pre=p;
p=p->next;
}
return r;
}
};结果:

方法二:
首先,排除链表为空或者只有一个数的情况。
将头节点断开,指针p遍历链表,每经过一个节点,先把它后继节点暂存,然后把它放到头节点的前面,头节点变成该节点,p变成后继节点,依次循环
代码:
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(head==nullptr||head->next==nullptr) return head;
ListNode *h,*temp,*p;
h=head;
p=head->next;
h->next=nullptr;
while(p){
temp=p->next;
p->next=head;
head=p;
p=temp;
}
return head;
}
};结果:

剑指 Offer 25. 合并两个排序的链表
题解:
首先,排除链表为空的情况。
从两个表中确定头节点。
依次比较两个链表的节点大小,将小的那个添加到新表。
最后判断两个表是否都已经走完。
返回新表头节点。
代码:
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if(l1==nullptr) return l2;
if(l2==nullptr) return l1;
ListNode *head,*p;
if(l1->val<l2->val){
head=l1;
l1=l1->next;
}
else{
head=l2;
l2=l2->next;
}
p=head;
while(l1!=nullptr&&l2!=nullptr){
if(l1->val<l2->val){
p->next=l1;
l1=l1->next;
}
else{
p->next=l2;
l2=l2->next;
}
p=p->next;
}
if(l1!=nullptr) p->next=l1;
if(l2!=nullptr) p->next=l2;
return head;
}
};
结果:

边栏推荐
- Nebula Graph入驻阿里云计算巢,助力企业打造云上超大规模图数据库
- After the 80 version of Google browser, how to deal with the problem samesite cross domain problem
- 508. Most Frequent Subtree Sum
- 【面试高频题】难度 1.5/5,常见的二分双指针面试题
- What is the process of futures account opening? Is it safe to open an account online
- nacos-配置中心-源码
- Nebula Graph入驻阿里云计算巢,助力企业打造云上超大规模图数据库
- 网管型全国产加固交换机如何创建网络冗余
- Flink 系例 之 TableAPI & SQL 與 示例模塊
- Flink 系例 之 TableAPI & SQL 与 示例模块
猜你喜欢

Nebula Graph入驻阿里云计算巢,助力企业打造云上超大规模图数据库

谷歌浏览器80版本以后,如何处理出现的问题SameSite跨域问题

CPDA|数据分析师需要具备哪些基本功?

使用uniapp框架搭建浙里办微应用(单点登录、埋点、适老化、RPC网关)

机器学习之神经网络与支持向量机

508. Most Frequent Subtree Sum

API interface for discharge summary identification - medical bill OCR identification / discharge diagnosis record / electronic medical record / claim settlement service

一次 MySQL 误操作导致的事故,「高可用」都顶不住了!

Huawei launches new products again? These models have excellent functions

在Qt中设置程序图标的方法介绍
随机推荐
动态规划【一】(背包问题)
How many correct answers can you get to Huawei Hongmeng certification test questions?
机器学习之线性模型与决策树
Kubernetes 跨 StorageClass 迁移 Persistent Volumes 完全指南
机器学习之贝叶斯分类与集成学习
[force deduction 10 days SQL introduction] Day1
Technology sharing | mysql:caching_ sha2_ Password quick Q & A
C. Helping the Nature(cf)差分
GetEmptyBlcoksPre Info
Use the uniapp framework to build the zheliban micro application (single sign on, embedded point, aging adaptation, RPC gateway)
After Hongmeng, Huawei announced that it would donate to Euler again. What impact is expected to be brought to the industry by the donations of Hongmeng and Euler?
如何使用DevExpress WPF在WinUI中创建第一个MVVM应用?
The main data products of China's two Fengyun meteorological "new stars" will be open and shared with global users
Nacos configuration center source code
R语言使用epiDisplay包的dotplot函数通过点图的形式可视化不同区间数据点的频率、使用by参数指定分组参数可视化不同分组的点图分布、使用cex.main参数指定可视化图像标题文本字体的大小
6月22日直播 | 华南理工詹志辉: 面向昂贵优化的进化计算
Double pointer 1day8 of daily practice of Li Kou
The GLM function of R language is used to build a binary logistic regression model (the family parameter is binomial), and the coef function is used to obtain the model coefficients and analyze the me
2022年6月25日PMP考试通关宝典-5
Forwarding to remind metamask how to deal with the potential private key disclosure of the expansion program