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;
    }
};
相关推荐
躺着听Jay5 分钟前
QCustomPlot-相关优化
java·qt·算法
扫地僧00916 分钟前
【中大厂面试题】腾讯 后端 校招 最新面试题
java·数据结构·后端·算法·面试·排序算法
qystca39 分钟前
二分答案----
算法·二分
编程绿豆侠1 小时前
力扣HOT100之链表:138. 随机链表的复制
算法·leetcode·链表
uhakadotcom1 小时前
JAX 框架:高性能数值计算的新时代
算法·面试·github
uhakadotcom2 小时前
构建实时API智能代理:快速构建多代理语音应用
算法·面试·github
uhakadotcom2 小时前
快速理解 tiktoken:OpenAI 的高效文本编码工具
算法·面试·github
这个懒人2 小时前
linux下io操作详细解析
开发语言·c++·io
做人求其滴2 小时前
蓝桥杯C/C++省赛/国赛注意事项及运行环境配置
算法·蓝桥杯·编译器·c/c++·算法竞赛·运行环境·第十六届
写个博客2 小时前
代码随想录算法训练营第十五天
算法