carl刷题 链表模块

1.移除元素   特殊节点的处理需要记住

class Solution
{
public:
    ListNode* removeElements(ListNode* head,int val){
        if(head==nullptr){
            return nullptr;
        }
        //如果头节点满足 需要特殊处理
        while (head->val==val)
        {
            ListNode* tmp=head;
            head=head->next;
            delete tmp;
            tmp=nullptr;
        }
        
        ListNode* cur=head;
        //不要设置快慢指针 直接用cur->next去判断
        while (cur!=nullptr&&cur->next!=nullptr)
        {
            /* code */
            if (cur->next->val==val)
            {
                ListNode *tmp=cur->next;
                cur->next=cur->next->next;
                delete tmp;
                tmp=nullptr;
                continue;
            }
            cur=cur->next;
        }
        return head;
    }
};

2.设计链表 看看得了


class MyLinkedList
{
public:
    MyLinkedList()
    {
        this->size = 0;
        this->head = new ListNode(0);
    }
    int get(int index)
    {
        if (index < 0 || index >= size)
            return -1;
        ListNode *p = head;
        for (int i = 0; i <=index; i++)
        {
            /* code */
            p=p->next;
        }
        return p->val;
    }
    void addAtHead(int val)
    {
        addAtIndex(0, val);
    }
    void addAtTail(int val)
    {
        addAtIndex(size, val);
    }
    void addAtIndex(int index, int val)
    {
        if (index > size)
        {
            /* code */
            return;
        }
        index = max(0, index);
        size++;
        ListNode *pred = head;
        for (int i = 0; i < index; i++)
        {
            /* code */
            pred = pred->next;
        }
        ListNode *toAdd = new ListNode(val);
        toAdd->next = pred->next;
        pred->next = toAdd;
    }
    void deleteAtIndex(int index)
    {
        if (index < 0 || index >= size)
        {
            /* code */
            return;
        }

        size--;
        ListNode *pred = head;
        for (int i = 0; i < index; i++)
        {
            /* code */
            pred = pred->next;
        }
        ListNode *p = pred->next;
        pred->next = pred->next->next;
        delete p;
        p = nullptr;
    }

private:
    int size;
    ListNode *head;
};

3.两两交换节点

class Solution
{
public:
    ListNode *swapPairs(ListNode *head)
    {
        if (head == nullptr)
        {
            return nullptr;
        }
        ListNode *cur = head;
        ListNode *pre = nullptr;
        int flag = 0;
        while (cur != nullptr && cur->next != nullptr)
        {
            flag++;
            ListNode *temp = cur->next;
            cur->next = cur->next->next;
            temp->next = cur;
            if (flag == 1)
            {
                /* code */
                head = temp;
            }
            else
            {
                pre->next = temp;
            }
            pre = cur;
            cur = cur->next;
        }
        return head;
    }
};

6.删除倒数第N个节点


class Solution
{
public:
    ListNode *removeNthFromEnd(ListNode *head, int n)
    {
        if (head == nullptr)
        {
            return nullptr;
        }

        int count = 0;
        ListNode *p = head;
        ListNode *q = head;
        // 先走N步
        while (p != nullptr)
        {
            count++;
            p = p->next;
            if (count == n)
            {
                break;
            }
        }
        if (count < n)
        {
            /* code */
            // 表明个数小于n
            return head;
        }

        ListNode *pre = q;
        
        //特殊处理下删的是头结点
        if (p==nullptr)
        {
            return head->next;
        }
        
        while (p != nullptr)
        {
            p = p->next;
            pre = q;
            q = q->next;
            if (p == nullptr)
            {
                /* code */
                pre->next = q->next;
                delete q;
                q=nullptr;
                return head;
            }
        }
        return head;
    }

private:
    int size;
    ListNode *head;
};

7.链表相交

class Solution
{
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB){
        int lenA=0,lenB=0;
        ListNode *p=headA;
        ListNode *curA=headA;
        ListNode *curB=headB;
        while (p!=nullptr)
        {
            lenA++;
            p=p->next;
        }
        p=headB;
        while (p!=nullptr)
        {
            lenB++;
            p=p->next;
        }
        
        //强制A的长一些
        if (lenA<lenB)
        {
            swap(lenA,lenB);
            swap(curA,curB);
        }
        int n=lenA-lenB;
        while (n--)
        {
            /* code */
            curA=curA->next;
        }
        while (curA!=nullptr&&curB!=nullptr)
        {
            if (curA==curB)
            {
                return curA;
            }
            curA=curA->next;
            curB=curB->next;
        }
        return nullptr;
    }
    
};

8.环形链表ii   记住就是快慢指针一定会相遇  相遇完从头开始一步一步走

class Solution
{
public:
    ListNode *detectCycle(ListNode *head)
    {
        // 先判断是否有环
        ListNode *fast = head;
        ListNode *slow = head;
        while (fast != nullptr && fast->next != nullptr)
        {
            /* code */
            //可能快指针转了很多圈
            slow = slow->next;
            fast = fast->next->next;
            // 快慢指针相遇
            if (slow == fast)
            {
                /* code */
                ListNode *index1 = fast;
                ListNode *index2 = head;
                while (index1 != index2)
                {
                    /* code */
                    index1 = index1->next;
                    index2 = index2->next;
                }
                return index2;
            }
        }
        //没有环
        return nullptr;
    }
};

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值