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;
    }
};
相关推荐
Erik_LinX2 分钟前
算法日记27:完全背包(DFS->记忆化搜索->倒叙DP->顺序DP->空间优化)
算法·深度优先
AC使者2 分钟前
D. C05.L08.贪心算法入门(一).课堂练习4.危险的实验(NHOI2015初中)
算法·贪心算法
wanjiazhongqi7 分钟前
链表和STL —— list 【复习笔记】
数据结构·c++·笔记·链表
-sky-117 分钟前
2.21作业
开发语言·c++·算法
li星野18 分钟前
std::thread的同步机制
开发语言·c++·学习
kgduu20 分钟前
遗传算法初探
算法
HL_LOVE_C22 分钟前
全面理解-回调函数CallBack
开发语言·c++
Distance失落心27 分钟前
java基于数组实现队列(四)
java·开发语言·数据结构·算法·面试·java-ee·intellij-idea
当归102432 分钟前
接雨水的算法
android·java·算法
深图智能40 分钟前
VS2022配置FFMPEG库基础教程
c++·计算机视觉·ffmpeg