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;
    }
};
相关推荐
PAK向日葵26 分钟前
【算法导论】PDD 0817笔试题题解
算法·面试
地平线开发者3 小时前
ReID/OSNet 算法模型量化转换实践
算法·自动驾驶
快乐的划水a3 小时前
组合模式及优化
c++·设计模式·组合模式
地平线开发者3 小时前
开发者说|EmbodiedGen:为具身智能打造可交互3D世界生成引擎
算法·自动驾驶
星星火柴9364 小时前
关于“双指针法“的总结
数据结构·c++·笔记·学习·算法
艾莉丝努力练剑5 小时前
【洛谷刷题】用C语言和C++做一些入门题,练习洛谷IDE模式:分支机构(一)
c语言·开发语言·数据结构·c++·学习·算法
C++、Java和Python的菜鸟7 小时前
第六章 统计初步
算法·机器学习·概率论
Cx330❀7 小时前
【数据结构初阶】--排序(五):计数排序,排序算法复杂度对比和稳定性分析
c语言·数据结构·经验分享·笔记·算法·排序算法
散1127 小时前
01数据结构-Prim算法
数据结构·算法·图论
起个昵称吧7 小时前
线程相关编程、线程间通信、互斥锁
linux·算法