当前位置:网站首页>刷题《剑指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;
}
};
边栏推荐
- umi3.5新特性之提速方案mfsu
- 学习用于视觉跟踪的深度紧凑图像表示Learning a Deep Compact Image Representation for Visual Tracking
- 概率论基础 - 10 - 常见概率分布
- Typora过期 报错:This beta version of Typora is expired, please download and install a newer version.
- 马氏距离 (马哈拉诺比斯距离) (Mahalanobis distance)
- 业务局部多线程
- Lagrange Multiplier Method
- 消失的遗传力的进一步剖分及应用
- 灵活好用的sql monitoring 脚本 part2
- 现在flink cdc有什么方式支持整库同步mysql吗
猜你喜欢

The Hyper - V virtualization vmware data recovery 】 【 file is missing, virtualization server unavailable data recovery case

学习笔记251—XMind快捷键汇总

Spine换装方案解析

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

Read it all!Adapter technology in NLP

训练好的神经网络怎么用,神经网络训练电脑配置

Observation cloud product update|DCA web terminal is online; new global viewer auto refresh configuration; new global blacklist function; new custom function menu, etc.

Study Notes 180—Relationship and Difference Between Regression Coefficient and Correlation Coefficient

Voice Chat App Development - How Developers Do Code Analysis

实战:JVM垃圾回收
随机推荐
深度卷积神经网络是什么,卷积神经网络结构设计
数据大屏rem适配方案
Matplotlib draws histogram
双因子与多因子身份验证有什么区别?
sklearn Notes: PCA
就是比某老师详细(反PPT系列)系列———自定义类型(下)
概率论基础 - 6 - 切比雪夫不等式
学习笔记251—XMind快捷键汇总
JS--如何编写事件驱动
正交-不相关-独立
Study Notes 251—XMind Shortcuts Summary
Ruoyi从mysql切换到postgresql的几个坑
一文读懂!NLP中的Adapter技术
恶访、黑产猖獗,做安全“守门人”| 创新场景50
Snake game on June 1st gift for everyone
图神经网络 图像处理,为什么用图神经网络
神经网络的原理和应用,神经网络理论及应用
d rebinding unchanged
编译器工程师眼中的好代码:Loop Interchange
语音聊天app开发——开发人员如何进行代码分析