141. Linked List Cycle

判断list是否存在环结构,利用快慢指针即可,快指针每次走两步,慢指针每次走一步

    bool hasCycle(ListNode *head) {
        if(!head) return false;
        ListNode* fast = head;
        ListNode* slow = head;
        while(fast && fast->next){
            slow = slow->next;
            fast = fast->next->next;
            if (fast==slow)
                return true;
        }
        return false;
    }

203. Remove Linked List Elements

#include <vector>
#include <iostream>
#include <time.h>
#include <ctime>

using namespace std;

struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};

class Solution {
public:
    // 递归实现
    ListNode* removeElements(ListNode* head, int val) {
            if(head == NULL) return head;
            ListNode* nextNode = removeElements(head->next, val);
            if(head->val == val) return nextNode;
            else {
                head->next = nextNode;
                return head;
            }
     }
    ListNode* Creat_list(vector<int> node) {
        ListNode* head = NULL,*tmp = NULL;

        int len = node.size();

        for(int i=0;i<len;i++){
            if (i==0){
                head = new ListNode(node[i]);
                head->next=NULL;
                tmp = head;
            }else{
                ListNode* new_op = new ListNode(node[i]);
                new_op->next = NULL;
                tmp->next = new_op;
                tmp = tmp->next;
            }

        }
        return head;
    }

};
// 测试用例
int main() {
    clock_t startTime,endTime;

    vector<int> v = {6,6,1,2,6,3,4,6,5};
    ListNode *head = Solution().Creat_list(v);

    startTime = clock();
    ListNode* ret = Solution().removeElements(head,6);
    endTime = clock();
    cout << "The run time is: " <<(double)(endTime - startTime) / CLOCKS_PER_SEC << "s" << endl;

    while (ret!=NULL){
        printf("%d",ret->val);
        ret = ret->next;
    }
    return 0;
}

非递归实现

    // 非递归 提前处理头结点
    ListNode* removeElements_2(ListNode* head, int val) {
        if (!head) return head;
        while (head != NULL && head->val == val) {
            head = head->next;
        }
        ListNode *ptr = NULL;
        ListNode *cur = head;
        while (cur) {
            if (cur->val == val) ptr->next = cur->next;
            else {
                ptr = cur;
            }
            cur = cur->next;
        }
        return head;
    }

    // 非递归 中间处理头结点
    ListNode* removeElements_3(ListNode* head, int val) {
        if (!head) return head;
        ListNode *ptr = NULL;
        ListNode *cur = head;
        while (cur) {
            if (cur->val == val) {
                if (!ptr) {
                    head = head->next;
                    cur = head;
                } else {
                    ptr->next = cur->next;
                    cur = cur->next;
                }
            } else {
                ptr = cur;
                cur = cur->next;
            }
        }
        return head;
    }
// 3.
 	if(head == NULL)  return NULL;
       ListNode* ptr=head;
        while(ptr->next!=NULL)
        {
            if(ptr->next->val==val)
            {
                ptr->next=ptr->next->next;
            }
            else{
                ptr=ptr->next;}   
        }
        
        if(head->val == val) head = head->next;
       
        return head;
        

142. Linked List Cycle II


class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        if (!head) return NULL;
        map<ListNode*,bool> m;
        ListNode* cur = head;
        int index = 0;
        while(cur){
            if (m[cur]==1)
                return cur;
            m[cur] = 1;
            cur = cur->next;   
        }
        return NULL;
    }
};