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

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的下一个为空结束
    }
};
相关推荐
Two_brushes.5 小时前
【算法】宽度优先遍历BFS
算法·leetcode·哈希算法·宽度优先
杰克尼11 小时前
BM5 合并k个已排序的链表
数据结构·算法·链表
xiaolang_8616_wjl11 小时前
c++文字游戏_闯关打怪
开发语言·数据结构·c++·算法·c++20
hqxstudying12 小时前
Java创建型模式---单例模式
java·数据结构·设计模式·代码规范
sun00770012 小时前
数据结构——栈的讲解(超详细)
数据结构
凌肖战15 小时前
力扣网编程55题:跳跃游戏之逆向思维
算法·leetcode
黑听人16 小时前
【力扣 简单 C】70. 爬楼梯
c语言·leetcode
ゞ 正在缓冲99%…17 小时前
leetcode918.环形子数组的最大和
数据结构·算法·leetcode·动态规划
Kaltistss18 小时前
98.验证二叉搜索树
算法·leetcode·职场和发展
努力写代码的熊大18 小时前
单链表和双向链表
数据结构·链表