代码随想录leetcode刷题60天之三

203.移除链表元素

力扣

自我解释:就是移除一个链表中的一个元素,比较关键的点是如果是第一元素是不是需要另外判断一下,所以如果用一个比较统一的方式来做这一道题的话,就是加一个dummy head。

有几个地方第一次写的时候是错的, delete这个写错了,else忘记写了,

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) 
    {
        if(head == NULL)
        {
            return head;
        }
        // step 1. new a dummy head
        ListNode* dummy_head = new ListNode(0);
        // step 2. put the dummy head next point to the head
        dummy_head->next = head;
        // step 3. make the dummy head as current head
        ListNode* curr_node = dummy_head;

        while(curr_node->next != NULL){
            if(curr_node->next->val == val)
            {
                //step 4. to delete
                ListNode* temp = curr_node->next;
                curr_node->next = curr_node->next->next;
                delete temp;
            }
            else
            {
                //step 5. remember the in the else
                curr_node = curr_node->next;

            }
        }

        ListNode* res = dummy_head->next;
        //step 6. delete the dummy head
        delete dummy_head;
        return res;
    }
};

707.设计链表

力扣

这个是个基础题,但是有些边界如果没有注意的话也会出错

class MyLinkedList {
public:

    struct LinkedNode
    {
        int val;
        LinkedNode* next;
        LinkedNode(int val):val(val), next(nullptr)
        {}
    };

    MyLinkedList() {
        dummy_head_ = new LinkedNode(0);
        size_ = 0;

    }
    
    int get(int index) {
        //1. 这个边界条件错了
        if (index > size_-1 ||index <0)
        {
            return -1;
        }

        LinkedNode* cur = dummy_head_->next;
        while(index --)
        {
            cur = cur->next;
        }
        return cur->val;
    }
    
    void addAtHead(int val) {
        LinkedNode* new_node = new LinkedNode(val);
        new_node->next = dummy_head_->next;
        dummy_head_->next = new_node;
        size_++;
    }
    
    void addAtTail(int val) {
        LinkedNode* new_node = new LinkedNode(val);
        LinkedNode* cur = dummy_head_;
        while(cur->next != NULL)
        {
            cur = cur->next;
        }
        cur->next  = new_node;
        size_++;

    }
    
    void addAtIndex(int index, int val) {
        if(index > size_ || index < 0)
        {
            return;
        }
        LinkedNode* new_node = new LinkedNode(val);
        LinkedNode* cur = dummy_head_;
        while(index--)
        {
            cur = cur->next;
        }
        new_node->next = cur->next;
        cur->next = new_node;
        size_++;
    }
    
    void deleteAtIndex(int index) {
        //这个边界条件少了一个=
        if(index>=size_ || index <0)
        {
            return;
        }
        LinkedNode* cur = dummy_head_;
        while(index--)
        {
            cur = cur->next;
        }
        LinkedNode* temp = cur->next;
        cur->next = cur->next->next;
        // delete temp;
        size_--;
    }
private:
    LinkedNode* dummy_head_;
    int size_;
};

/**
 * Your MyLinkedList object will be instantiated and called as such:
 * MyLinkedList* obj = new MyLinkedList();
 * int param_1 = obj->get(index);
 * obj->addAtHead(val);
 * obj->addAtTail(val);
 * obj->addAtIndex(index,val);
 * obj->deleteAtIndex(index);
 */

24.反转链表

力扣

这道题应该算比较基础的题。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if(head == NULL)
        {
            return head;
        }
        // prev
        ListNode* prev = NULL;
        // curr
        ListNode* curr = head;
        // temp
        ListNode* temp;
        while(curr)
        {
            temp = curr->next;
            curr->next= prev;

            prev = curr;
            curr = temp;
        }
        return prev;

    }
};

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值