【算法训练营day4】LeetCode24. 两两交换链表中的结点 LeetCode19. 删除链表的倒数第N个结点 LeetCode面试题 02.07. 链表相交 LeetCode142. 环形链表II
【算法训练营day4】LeetCode24. 两两交换链表中的结点 LeetCode19. 删除链表的倒数第N个结点 LeetCode面试题 02.07. 链表相交 LeetCode142. 环形链表II
LeetCode24. 两两交换链表中的节点
题目链接:24. 两两交换链表中的节点
初次尝试
比较暴力的解法,利用三个指针,进行类似反转链表里面的反转next指针指向的操作,然后三个指针整体向后移动到下一组节点,暴力但是ac。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if (head == NULL || head -> next == NULL) return head;
ListNode* pre = head;
ListNode* cur = head -> next;
ListNode* temp = cur -> next;
head = head -> next;
while (true) {
cur -> next = pre;
pre -> next = temp;
if (temp != NULL && temp -> next != NULL) {
cur = temp -> next;
pre -> next = cur;
pre = temp;
temp = cur -> next;
}
else break;
}
return head;
}
};
看完代码随想录后的想法
思路差不多,忘记用虚拟头结点了,重新用虚拟头结点写了一下,ac。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if (head == NULL || head -> next == NULL) return head;
ListNode* dummyHead = new ListNode(0, head);
ListNode* cur = dummyHead;
while (cur -> next != NULL && cur -> next -> next != NULL) {
ListNode* temp1 = cur -> next;
ListNode* temp2 = cur -> next -> next;
cur -> next = temp2;
temp1 -> next = temp2 -> next;
temp2 -> next = temp1;
cur = cur -> next -> next;
}
return dummyHead -> next;
}
};
LeetCode19. 删除链表的倒数第N个结点
题目链接:19. 删除链表的倒数第N个结点
初次尝试
暴力解法,先遍历链表得出链表的长度,然后计算出倒数第n个结点的索引,然后删除,ac。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* newHead = new ListNode(0, head);
ListNode* p = head;
int listSize = 0;
while (p != NULL) {
p = p -> next;
listSize++;
}
p = newHead;
ListNode* temp;
int delIndex = listSize - n;
while (delIndex > 0) {
p = p -> next;
delIndex--;
}
temp = p -> next;
p -> next = temp -> next;
delete temp;
return newHead -> next;
}
};
看完代码随想录后的想法
题解利用了快慢指针,快指针先移动n个结点,然后快慢指针同时移动,在移动过程中保持n距离不变,这样快指针到达链表尾部的时候,慢指针就正好位于要删除的结点,十分巧妙的思路,重新写一遍ac。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* dummyHead = new ListNode(0, head);
ListNode* fast = dummyHead;
ListNode* slow = dummyHead;
for (; n > 0; n--) {
fast = fast -> next;
}
while (fast -> next != NULL) {
fast = fast -> next;
slow = slow -> next;
}
ListNode* temp = slow -> next;
slow -> next = temp -> next;
delete temp;
return dummyHead -> next;
}
};
LeetCode面试题 02.07. 链表相交
题目链接:面试题 02.07. 链表相交
初次尝试
没有想到解法,思路卡在了单链表不能回溯上面。
看完代码随想录后的想法
题解考虑到了两个链表的长度差,感觉和今天的第二题有异曲同工之妙,虽然单链表不能回溯,但是如果能提前知道需要回溯的长度,在第二题中就是倒数第n个,在本题中就是两个链表长度的差,这就是这类题中核心的“不变量”,通过使用保持这个距离同时移动的快慢指针,就可以从单链表不能回溯的思维定势中走出来。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode* pA = headA;
ListNode* pB = headB;
int aSize = 0, bSize = 0;
while (pA != NULL) {
aSize++;
pA = pA -> next;
}
while (pB != NULL) {
bSize++;
pB = pB -> next;
}
int offset = aSize - bSize;
pA = headA;
pB = headB;
if (offset >= 0) {
for (; offset != 0; offset--) {
pA = pA -> next;
}
}
else {
for (; offset != 0; offset++) {
pB = pB -> next;
}
}
while (pA != NULL) {
if (pA == pB) return pA;
pA = pA -> next;
pB = pB -> next;
}
return NULL;
}
};
LeetCode142. 环形链表II
题目链接:142. 环形链表II
初次尝试
没有想到解法,两年前见过用快慢指针解环链表问题的解法,解本题的时候也一直想怎么用来着?最后还是没有想起来具体是怎么用的。
看完代码随想录后的想法
刚看到需要数学计算的提示,就开始想自己先算一算,一开始以为是可以通过数学计算直接解出入环结点的索引,所以一直致力于解出具体的数字,后来发现解不出来就放弃了,完整看了视频题解,解法真的很巧妙,并没有解出什么具体的数字,而是通过理解等式本身的意义,得出了获得入环结点索引的方法,虽然这个解法不具有大规模的应用能力,但是很锻炼思维能力和计算能力。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
if (head == NULL || head -> next == NULL) return NULL;
ListNode* fast = head;
ListNode* slow = head;
while (fast != NULL && fast -> next != NULL) {
fast = fast -> next -> next;
slow = slow -> next;
if (fast == slow) {
ListNode* index1 = head;
ListNode* index2 = fast;
while (index1 != index2) {
index1 = index1 -> next;
index2 = index2 -> next;
}
return index1;
}
}
return NULL;
}
};
【算法训练营day4】LeetCode24. 两两交换链表中的结点 LeetCode19. 删除链表的倒数第N个结点 LeetCode面试题 02.07. 链表相交 LeetCode142. 环形链表II的更多相关文章
- LeetCode 面试题 02.07. 链表相交
题目链接:https://leetcode-cn.com/problems/intersection-of-two-linked-lists-lcci/ 给定两个(单向)链表,判定它们是否相交并返回交 ...
- leetcode面试题 02.06. 回文链表,解题心路
目录 leetcode面试题 02.06. 回文链表,解题心路 1.题目描述 2.java语言题解一 3.java语言题解二 4.C语言题解一 leetcode面试题 02.06. 回文链表,解题心路 ...
- SDUT OJ 数据结构实验之链表七:单链表中重复元素的删除
数据结构实验之链表七:单链表中重复元素的删除 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem ...
- SDUT-2122_数据结构实验之链表七:单链表中重复元素的删除
数据结构实验之链表七:单链表中重复元素的删除 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 按照数据输入的相反顺序(逆 ...
- LeetCode142 环形链表 II
给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 说明:不允许修改给定的链表. 进阶:你是否可以不用额外空间解决此题? //章节 - 链表 //二.双指针技巧 //2.环 ...
- 【leetcode 简单】 第三十五题 环形链表
给定一个链表,判断链表中是否有环. 进阶: 你能否不使用额外空间解决此题? /** * Definition for singly-linked list. * struct ListNode { * ...
- Leetcode142 环形链表
很多题解没有讲清楚非环部分的长度与相遇点到环起点那部分环之间为何是相等的这个数学关系.这里我就补充下为何他们是相等的.假设非环部分的长度是x,从环起点到相遇点的长度是y.环的长度是c.现在走的慢的那个 ...
- LeetCode 面试题 02.06. 回文链表
题目链接:https://leetcode-cn.com/problems/palindrome-linked-list-lcci/ 编写一个函数,检查输入的链表是否是回文的. 示例 1: 输入: 1 ...
- [Swift]LeetCode142. 环形链表 II | Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note ...
随机推荐
- Python 阿里云盾滑块验证
本文仅供学习交流使用,如侵立删! 记一次阿里云盾滑块验证分析并通过 操作环境 win10 . mac Python3.9 selenium.pyautogui 分析 最近在做中国庭审公开网数据分析的时 ...
- 二手车价格预测 | 构建AI模型并部署Web应用 ⛵
作者:韩信子@ShowMeAI 数据分析实战系列:https://www.showmeai.tech/tutorials/40 机器学习实战系列:https://www.showmeai.tech/t ...
- 完成 DolphinScheduler 新手任务赢好礼活动 | 倒计时3 天
想轻松参与 DolphinScheduler 项目贡献吗? 想获得 500 元京东购物卡吗? 参与活动,有机会得更多活动奖励! 活动截止至6月30日 了解更多详情: 在你参与 DolphinSched ...
- 查看 npm 的全局安装依赖包
在控制台中输入以下指令可以直接查看 npm 全局安装的依赖包: npm list -g --depth 0
- C++11实现的数据库连接池
它什么是? 数据库连接池负责分配.管理和释放数据库连接,它允许应用程序重复使用一个现有的数据库连接,而不是再重新建立一个:类似的还有线程池. 为什么要用? 一个数据库连接对象均对应一个物理数据库连接, ...
- [CF1498D] Bananas in a Microwave (DP)
题面&翻译 题解 虽然 m m m 很大,但是 n n n 很小,因此题目允许我们在 O ( n m ) O(nm) O(nm) 以内解决这道题. 定义一个 dp[i][j]=0/1 ? 如果 ...
- nginx的安装和配置
目录 目录 一.购买下载SSL证书 二.修改Nginx配置信息 三.重启Nginx 一.购买下载SSL证书 SSL证书阿里云做活动期间可以免费申请,购买SSL证书时选择单域名-DV SSL-免费版即可 ...
- Android平台Camera2数据如何对接RTMP推流到服务器
1. Camera2架构 在Google 推出Android 5.0的时候, Android Camera API 版本升级到了API2(android.hardware.camera2), 之前使用 ...
- ConcurrentDictionary<T,V> 的这两个操作不是原子性的
好久不见,马甲哥封闭居家半个月,记录之前遇到的一件小事. ConcurrentDictionary<TKey,TValue>绝大部分api都是线程安全且原子性的, 唯二的例外是接收工厂委托 ...
- Pixar 故事公式
文章转载自:https://mp.weixin.qq.com/s/wMfFVh9tAM5Qo4ED658yUg