当前位置:网站首页>刷题《剑指Offer》day09
刷题《剑指Offer》day09
2022-08-05 15:02:00 【吃豆人编程】
题目来源:力扣《剑指Offer》第二版
完成时间:2022/07/31
22. 链表中倒数第k个节点

我的题解
这题比较简单,用栈实现即可。另外还有快慢指针的解法,空间复杂度O(1),比用栈实现更好,这里就不写了。
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */
class Solution {
public:
ListNode* getKthFromEnd(ListNode* head, int k) {
stack<ListNode*> s1;
ListNode* tmp = head;
while(tmp != nullptr) {
s1.push(tmp);
tmp = tmp->next;
}
int count = 1;
while(count < k) {
s1.pop();
count++;
}
return s1.top();
}
};
24. 反转链表

我的题解
这题要用三指针,tmp指针用来遍历整个链表,cur和pre用来实现相邻两个节点的反转。
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* cur = head;
ListNode* pre = nullptr;
ListNode* tmp = nullptr;
while(cur != nullptr){
tmp = cur->next;
cur->next = pre;
pre = cur;
cur = tmp;
}
return pre;
}
};
递归写法
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */
class Solution {
public:
ListNode* reverse(ListNode* pre,ListNode* cur){
if(cur == NULL) return pre;
ListNode* tmp = cur->next;
cur->next = pre;
return reverse(cur,tmp);
}
ListNode* reverseList(ListNode* head) {
return reverse(NULL,head);
}
};
其他题解
这种递归的方式更简洁,但是要注意需要多定义一个tmp指针用来接收反转之后的头指针。
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(head == nullptr || head->next == nullptr) {
return head;
}
ListNode* tmp = reverseList(head->next);
head->next->next = head;
head->next = nullptr;
return tmp;
}
};
25. 合并两个排序的链表

我的题解
这题也比较简单,直接上代码
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* head = new ListNode(-1);
ListNode* pre = head;
while(l1 != NULL && l2 != NULL){
if(l1->val > l2->val){
pre->next = l2;
l2 = l2->next;
}else {
pre->next = l1;
l1 = l1->next;
}
pre = pre->next;
}
if(l1 == NULL) pre->next = l2;
else pre->next = l1;
return head->next;
}
};
边栏推荐
猜你喜欢

Interview with Rongzhi Information Chai Yatuan: How do the most low-key companies make the most easy-to-use RPA?

自媒体人必看的9个网站,每一个都很实用,值得收藏

双因子与多因子身份验证有什么区别?

Matplotlib绘制直方图

It is more detailed than a certain teacher (anti-PPT series) series - custom type (below)

有什么可以代替Calendar的吗?

如何科学预测后代的身高

Read it all!Adapter technology in NLP

An in-depth long article discusses the simplification and speedup of JOIN operations

2022最新综述 | 面向大规模场景的小目标检测:综述和 benchmark
随机推荐
Business local multithreading
go语言的ini文件配置读取
Analysis of Rocket MQ Crash-Safe Mechanism
To be a famous corporate scientist or to be a tenured professor, this is a question
马氏距离 (马哈拉诺比斯距离) (Mahalanobis distance)
ES6解构详解
map 和 forEach 的区别
全同胞家系如何计算遗传力及育种值
d rebinding unchanged
Advanced roadmap for technical salary increase of full-stack software test engineers (with information)
Postgresql源码(67)LWLock锁的内存结构与初始化
一文读懂!NLP中的Adapter技术
全栈软件测试工程师技术涨薪进阶路径图(附资料)
利用PHP的特性做免杀Webshell
Oracle自治事务详解
兆骑科创高层次人才引进平台,赛事活动举办,线上路演
7 RESTful
【Navicat】Navicat导出数据库表设计文档具体说明
Integration testing of software testing
概率论基础 - 12 - 拉普拉斯分布(Laplace分布)