LeetCode热题100刷题6:160. 相交链表、206. 反转链表、234. 回文链表、141. 环形链表、142. 环形链表 II

160. 相交链表

cpp 复制代码
/**
 * 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* curA = headA;
        ListNode* curB = headB;

        int sizeA=0,sizeB=0;
        while(curA != nullptr) {
            sizeA++;
            curA = curA->next;
        }
        while(curB != nullptr) {
            sizeB++;
            curB = curB->next;
        }
        curA = headA;
        curB = headB;
        if(sizeA > sizeB) {
            int temp = sizeA - sizeB;
            while(temp) {
                curA = curA->next;
                temp--;
            }
        }
        else if(sizeA < sizeB) {
            int temp = sizeB - sizeA;
            while(temp) {
                curB = curB->next;
                temp--;
            }
        }

        while(curA != curB) {
            curA = curA->next;
            curB = curB->next;
        }
        if(curA == NULL || curB == NULL)
            return NULL;
        else 
            return curA;
    }
};

206. 反转链表

cpp 复制代码
/**
 * 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* reverseList(ListNode* head) {
        ListNode* dummyHead = new ListNode(0);
        dummyHead->next = nullptr;
        ListNode* cur = head;
        while(cur != nullptr) {
            ListNode* temp = cur->next;
            cur->next = dummyHead->next;
            dummyHead->next = cur;
            cur = temp;
        }
        return dummyHead->next;
    }
};

234. 回文链表

cpp 复制代码
/**
 * 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:
    bool isPalindrome(ListNode* head) {
        if(head==nullptr || head->next==nullptr)
            return true;
        vector<int> nums;
        while(head!=nullptr) {
            nums.push_back(head->val);
            head = head->next;
        }
        int size = nums.size();
        int i=0,j=size-1;
        while(i<j) {
            if(nums[i] == nums[j]) {
                i++;
                j--;
                continue;
            }
            else
                return false;
        }
        return true;
    }
};

141. 环形链表

cpp 复制代码
/**
 * 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) {
        if(head==nullptr || head->next == nullptr)
            return false;
        ListNode* fast = head;
        ListNode* slow = head;
        while(fast && fast->next) {   //这里保证fast->next也不为空是因为若为空 不存在->next了
            fast = fast->next->next;
            slow = slow->next;
            if(slow == fast)
                return true;
        }
        return false;
    }
};

142. 环形链表 II

加粗样式

cpp 复制代码
/**
 * 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) {
        if(head==nullptr || head->next == nullptr)
            return false;
        ListNode* fast = head;
        ListNode* slow = head;
        while(fast && fast->next) {   //这里保证fast->next也不为空是因为若为空 不存在->next了
            fast = fast->next->next;
            slow = slow->next;
            if(slow == fast)
                return true;
        }
        return false;
    }
};
相关推荐
HEX9CF7 分钟前
【Linux】awk 命令详解及使用示例:结构化文本数据处理工具
linux·chrome·算法
Cl_rown去掉l变成C21 分钟前
第J3-1周:DenseNet算法 实现乳腺癌识别
人工智能·pytorch·算法
努力学习的小廉21 分钟前
我爱学算法之—— 前缀和(中)
开发语言·redis·算法
保持学习ing23 分钟前
黑马Java面试笔记之 集合篇(算法复杂度+ArrayList+LinkedList)
java·笔记·算法·面试
呃m24 分钟前
加法c++
c++
LunaGeeking27 分钟前
三分算法与DeepSeek辅助证明是单峰函数
c语言·c++·算法·编程·信奥赛·ai辅助学习·三分
byte轻骑兵1 小时前
【Bluedroid】蓝牙启动之 SMP_Init 源码解析
android·c++·smp·bluedroid
Darkwanderor1 小时前
数论——同余问题全家桶3 __int128和同余方程组
c++·算法·数论·中国剩余定理
Xyz_Overlord1 小时前
机器学习——聚类算法
算法·机器学习·聚类
dessler1 小时前
代理服务器-LVS的3种模式与调度算法
运维·服务器·网络·算法·nginx·tomcat·lvs