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

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的下一个为空结束
    }
};
相关推荐
宁檬精13 小时前
算法练习——55.跳跃游戏
数据结构·算法·游戏
XFF不秃头14 小时前
力扣刷题笔记-三数之和
c++·笔记·算法·leetcode
高山有多高16 小时前
顺序表:数据结构中的基础线性存储结构
数据结构
默默无名的大学生16 小时前
数据结构——链表的基本操作
数据结构·算法
_OP_CHEN16 小时前
数据结构(C语言篇):(十一)二叉树概念介绍
c语言·开发语言·数据结构·二叉树·学习笔记··
Neverfadeaway16 小时前
C语言————冒泡排序(例题2)
c语言·数据结构·算法·冒泡排序·升序排列·降序排列
散11216 小时前
01数据结构-B树
数据结构·b树
亦良Cool16 小时前
001-Pandas的数据结构
数据结构·pandas
惊鸿.Jh16 小时前
1733. 需要教语言的最少人数
算法·leetcode
nsjqj17 小时前
数据结构中的 二叉树
数据结构