List_算法

博客主要围绕链表相关问题展开,涉及判断链表是否存在环结构,可利用快慢指针,快指针每次走两步,慢指针每次走一步;还提到移除链表元素的非递归实现,以及链表环结构的另一相关问题。

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;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值