【算法训练营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. html的基础01

    1.什么是网页 2.常用的浏览器有哪些 3.web标准是什么  1.什么是网页  2.常用的浏览器 360.百度那些都是国产浏览器,内核一样,以上六个都是国际浏览器,不同厂商生产(但IE和Edge都是 ...

  2. php static 和self区别

    static(关键字) 类似于 self(关键字) , 但它指向的是被调用的类(Document) 而不是包含类(DomainObject) , static 和 self 的区别: <?php ...

  3. mysql grant命令

    增删改查的权限grant select on testdb.* to common_user@'%' grant insert on testdb.* to common_user@'%' grant ...

  4. Multi-Party Threshold Private Set Intersection with Sublinear Communication-2021:解读

    记录阅读论文的笔记. 摘要 总结: (1)CRYPTO 2019:The Communication Complexity of Threshold Private Set Intersection- ...

  5. C# 虚方法、抽象方法

    一.虚方法(virtual) 作用:当有一个定义在类中的函数需要在继承类中实现时,可以使用虚方法. 示例: class Person { public virtual void XXX() { Con ...

  6. 运维实践-最新Nginx二进制构建编译lua-nginx-module动态链接Lua脚本访问Redis数据库读取静态资源隐式展现

    关注「WeiyiGeek」公众号 设为「特别关注」每天带你玩转网络安全运维.应用开发.物联网IOT学习! 希望各位看友[关注.点赞.评论.收藏.投币],助力每一个梦想. 本章目录 目录 0x0n 前言 ...

  7. MGR的gtid_executed不连续的问题分析

    GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源. 1.问题描述 在做MGR测试的时候偶尔遇到gtid_executed事务ID不连续的问题,但是并不影响数据库的正常运行.现 ...

  8. 创建Prism项目

    1.创建Prism Prism是一个用于WPF.Xamarin Form.Uno平台和 WinUI 中构建松散耦合.可维护和可测试的XAML应用程序框架 通过以下方式访问.使用.学习它: https: ...

  9. 详解MySQL隔离级别

    一个事务具有ACID特性,也就是(Atomicity.Consistency.Isolation.Durability,即原子性.一致性.隔离性.持久性),本文主要讲解一下其中的Isolation,也 ...

  10. 美丽的神话 flac 成龙/金喜善 美丽的神话 mp3 韩红/孙楠

    这里分享从网上收集的俩个版本的歌,都很不错,有兴趣的可以听听 以下是成龙/金喜善 flac 版本,音质不错: 美丽的神话成龙/金喜善解开我最神秘的等待星星坠落风在吹动终于再将你融入怀中两颗心颤抖相信我 ...