【算法训练营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的更多相关文章

  1. LeetCode 面试题 02.07. 链表相交

    题目链接:https://leetcode-cn.com/problems/intersection-of-two-linked-lists-lcci/ 给定两个(单向)链表,判定它们是否相交并返回交 ...

  2. leetcode面试题 02.06. 回文链表,解题心路

    目录 leetcode面试题 02.06. 回文链表,解题心路 1.题目描述 2.java语言题解一 3.java语言题解二 4.C语言题解一 leetcode面试题 02.06. 回文链表,解题心路 ...

  3. SDUT OJ 数据结构实验之链表七:单链表中重复元素的删除

    数据结构实验之链表七:单链表中重复元素的删除 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem ...

  4. SDUT-2122_数据结构实验之链表七:单链表中重复元素的删除

    数据结构实验之链表七:单链表中重复元素的删除 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 按照数据输入的相反顺序(逆 ...

  5. LeetCode142 环形链表 II

    给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 说明:不允许修改给定的链表. 进阶:你是否可以不用额外空间解决此题? //章节 - 链表 //二.双指针技巧 //2.环 ...

  6. 【leetcode 简单】 第三十五题 环形链表

    给定一个链表,判断链表中是否有环. 进阶: 你能否不使用额外空间解决此题? /** * Definition for singly-linked list. * struct ListNode { * ...

  7. Leetcode142 环形链表

    很多题解没有讲清楚非环部分的长度与相遇点到环起点那部分环之间为何是相等的这个数学关系.这里我就补充下为何他们是相等的.假设非环部分的长度是x,从环起点到相遇点的长度是y.环的长度是c.现在走的慢的那个 ...

  8. LeetCode 面试题 02.06. 回文链表

    题目链接:https://leetcode-cn.com/problems/palindrome-linked-list-lcci/ 编写一个函数,检查输入的链表是否是回文的. 示例 1: 输入: 1 ...

  9. [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 ...

随机推荐

  1. 在阿里云Centos7.6上利用docker搭建Jenkins来自动化部署Django项目

    原文转载自「刘悦的技术博客」https://v3u.cn/a_id_84 一般情况下,将一个项目部署到生产环境的流程如下: 需求分析-原型设计-开发代码-内网部署-提交测试-确认上线-备份数据-外网更 ...

  2. Mybatis的使用(4)

    1:解决实体类成员变量和数据库表中字段名称不一致的问题: 方法1:在写sql语句时,给表中的列名起别名,名字和实体类名称一样 方法2:使用resultMap来解决: 例如:实体类中成员变量为id,na ...

  3. git使用的一些坑和新得(一)

    这是一个坑 你要知道作为一个新手对git的使用还处于摸索状态 今天就将这样的坑分享给大家 昨天,接到任务将代码发到远程仓库里.于是,我就天真的按步骤提交了! 然后就: To https: ! [rej ...

  4. 回溯、贪心、DP的区别和联系

    四大常用算法:分治.贪心.回溯.动态规划 回溯算法是个"万金油".基本上能用跟动态规划.贪心解决的问题,都可以用回溯去解决.回溯算法相当于穷举搜索,穷举所有情况,然后得到最优解.不 ...

  5. 座位安排(欧拉回路,高斯消元,bitset)

    题面 由于旋转大师 F r e n c h \rm French French 的离去, A r e x t r e \rm Arextre Arextre 光荣地承担了给全班换座位的重任. 由于这是 ...

  6. 【mido】python的midi处理库

    安装mido库:pip install mido pipy地址:https://pypi.org/project/mido/ mido官方文档:https://mido.readthedocs.io/ ...

  7. KingbaseES R6 集群repmgr.conf参数'recovery'测试案例(一)

    KingbaseES R6集群repmgr.conf参数'recovery'测试案例(一) 案例说明: 在KingbaseES R6集群中,主库节点出现宕机(如重启或关机),会产生主备切换,但是当主库 ...

  8. Docker_构建_运行总结

    样例: 构建镜像 build-image-fim-backend.sh echo "开始构建 fim-backend 镜像..." cp -rp ../target/fim-bac ...

  9. Golang 随机淘汰算法缓存实现

    缓存如果写满, 它必须淘汰旧值以容纳新值, 最近最少使用淘汰算法 (LRU) 是一个不错的选择, 因为你如果最近使用过某些值, 这些值更可能被保留. 你如果构造一个比缓存限制还长的循环, 当循环最后的 ...

  10. SpringMVC--从理解SpringMVC执行流程到SSM框架整合

    前言 SpringMVC框架是SSM框架中继Spring另一个重要的框架,那么什么是SpringMVC,如何用SpringMVC来整合SSM框架呢?下面让我们详细的了解一下. 注:在学习SpringM ...