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;
    }
};
相关推荐
donotdothat15 分钟前
D1.排序
数据结构·算法
LabVIEW开发17 分钟前
LabVIEW透视变换
算法·计算机视觉·labview·labview开发
无敌海苔咪24 分钟前
实验六 图像的傅立叶变换
图像处理·算法·计算机视觉·matlab·数字图像处理
惜了梦i35 分钟前
【leetcode周赛记录——405】
算法·leetcode·职场和发展
danaaaa1 小时前
算法力扣刷题记录 二十八【225. 用队列实现栈】
数据结构·c++·算法·leetcode·职场和发展
未来可期,静待花开~1 小时前
C++基础(八):类和对象 (下)
开发语言·jvm·c++
范范08251 小时前
Scikit-learn高级教程:深入理解机器学习算法
算法·机器学习·scikit-learn
wx200411021 小时前
P1392 取数
算法·深度优先
2301_803361481 小时前
第一周周日总结
数据结构·算法
roo_11 小时前
3. 排序算法代码-python
python·算法·排序算法