链表快慢指针合集(力扣)

876. 链表的中间结点

代码

复制代码
/**
 * 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* middleNode(ListNode* head) {
         ListNode * slow = head, * fast = head;
        while(fast && fast -> next){
            slow = slow->next;
            fast = fast->next->next;
        }//找中间节点
        return slow;
    }
};

876. 链表的中间结点

代码

复制代码

141. 环形链表

代码

复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    bool hasCycle(ListNode *head) {
        ListNode * slow = head, * fast = head;
        while(fast && fast -> next){
            slow = slow->next;
            fast = fast -> next -> next;
            if(slow == fast) return true;
        }
        return false;

    }
};

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) {
        ListNode * slow = head, *fast = head;
        while(fast && fast -> next){
            slow = slow -> next;
            fast = fast -> next -> next;
            if(slow == fast){
                while(head != slow){
                    head = head->next;
                    slow = slow -> next;
                }
                return slow;
            }
        }
        return NULL;
    }
};

LCR 026. 重排链表

代码

复制代码
/**
 * 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:
    void reorderList(ListNode* head) {
        ListNode * slow = head, * fast = head;
        while(fast && fast -> next){
            slow = slow->next;
            fast = fast->next->next;
        }//找中间节点
        ListNode * prev = nullptr, * cur = slow;
        while(cur){
            ListNode * tp = cur -> next;
            cur -> next = prev;
            prev = cur;
            cur = tp;
        }//反转后半段链表
        ListNode * head2 = prev, *head1 = head;
        while(head2 -> next){
            ListNode * tp1 = head1->next, * tp2 = head2 -> next;
            head2->next = head1->next;
            head1->next = head2;
            head1 = tp1;
            head2 = tp2;
        }//令交错指向,最终head2的下一个为空结束
    }
};
相关推荐
快去睡觉~2 小时前
力扣73:矩阵置零
算法·leetcode·矩阵
岁忧3 小时前
(nice!!!)(LeetCode 每日一题) 679. 24 点游戏 (深度优先搜索)
java·c++·leetcode·游戏·go·深度优先
小欣加油3 小时前
leetcode 3 无重复字符的最长子串
c++·算法·leetcode
月盈缺6 小时前
学习嵌入式的第二十二天——数据结构——双向链表
数据结构·学习·链表
科大饭桶7 小时前
C++入门自学Day14-- Stack和Queue的自实现(适配器)
c语言·开发语言·数据结构·c++·容器
元亓亓亓8 小时前
LeetCode热题100--101. 对称二叉树--简单
算法·leetcode·职场和发展
躲在云朵里`8 小时前
深入理解数据结构:从数组、链表到B树家族
数据结构·b树
1白天的黑夜111 小时前
链表-24.两两交换链表中的结点-力扣(LeetCode)
数据结构·leetcode·链表
快去睡觉~14 小时前
力扣48:旋转矩阵
算法·leetcode·矩阵
卡洛斯(编程版16 小时前
(1) 哈希表全思路-20天刷完Leetcode Hot 100计划
python·算法·leetcode