代码随想录刷题第三天

本文介绍了如何在链表中进行元素删除,包括使用虚拟节点简化操作。同时展示了如何设计一个链表类,包含添加、获取、删除元素等方法。此外,还讨论了反转链表的算法,需要注意空链表的情况。

代码随想录刷题第三天

移除链表元素

/**
 * 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* removeElements(ListNode* head, int val) {
        ListNode *dummyhead = new ListNode();
        dummyhead->next = head;
        ListNode *cur = dummyhead;
        while (cur->next != nullptr)
        {
            if (cur->next->val == val)
            {
                cur->next = cur->next->next;
            }else{
                cur = cur->next;
            }
            
        }
        
        return dummyhead->next;
      // return head;
    }
};

就是正常的删除链表的基本操作,记得新建一个新的虚拟节点

设计链表

class MyLinkedList {
public:
    struct ListNode 
    {
        int val;
        ListNode *next;
        ListNode(int x): val(x),next(nullptr){}
    };
    
    MyLinkedList() {
        dummyhead = new ListNode(0);
        size = 0;
    }
    
    int get(int index) {
        if (index < 0 || index >=size)
        {
            return -1;
        }
        ListNode *cur = dummyhead->next;
        while (index--)
        {
            cur = cur->next;
        }
        return cur->val;
    }
    
    void addAtHead(int val) {
        ListNode *node = new ListNode(val);
        node->next = dummyhead->next;
        dummyhead->next = node;   
        size++;     
    }
    
    void addAtTail(int val) {
         ListNode *node = new ListNode(val);
         ListNode *cur = dummyhead;
         int size_tmp=  size;
         while (size_tmp--)
         {
            cur = cur->next;
         }
         cur->next = node;
         size++;
    }
    
    void addAtIndex(int index, int val) {
        if (index > size )
        {
            return;
        }
        
        ListNode *node = new ListNode(val);
        ListNode *cur = dummyhead;
        if (index < 0)
        {
            node->next = cur->next;
            cur->next = node;
            return ;
        }
        
        while (index--)
        {
            cur = cur->next;
        }
        node->next = cur->next;
        cur->next = node;
        size++;
    }
    
    void deleteAtIndex(int index) {

        if (index < 0 || index >= size)
        {
            return ;
        }
        
        ListNode *cur = dummyhead;
        while (index--)
        {
            cur = cur->next;
        }
        cur->next = cur->next->next;
        size--;
    }

private:
    int size;
    ListNode *dummyhead;
};

虚拟节点还有链表长度是很关键的点,加上后代码变得简洁很多

反转链表

class Solution {
    
public:
    ListNode* reverseList(ListNode* head) {
        if (head == nullptr)
        {
            return head;
        }
        
        ListNode *pre = nullptr;
        ListNode *tmp ;
        ListNode *cur = head;
        while (cur->next != nullptr)
        {
            tmp = cur->next;
            cur->next = pre ;
            pre = cur;
            cur = tmp;
        }
        cur->next = pre;
        return cur;
    }
};

不要忘记空链表的情况,前面要加一个判断

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值