代码随想录Day3(203.移除链表元素,707.设计链表,206.反转链表)

代码随想录Day3(203.移除链表元素,707.设计链表,206.反转链表)

203.移除链表元素

思路:head前加一个虚拟节点,保证移除head时保持一致。

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {

        ListNode* dummy = new ListNode();
        dummy->next = head;
        ListNode* cur = dummy;

        while(cur->next!=NULL){
            if(cur->next->val == val){
                cur->next = cur->next->next;
            }
            else{
                cur = cur->next;
            }           
        }
        return dummy->next;

    }
};

707.设计链表

思路:

class MyLinkedList {

public:
    struct LinkNode{
        int val;
        LinkNode* next;
        LinkNode(int x): val(x), next(nullptr){}

    };

    MyLinkedList() {
        dummy = new LinkNode(0);
        size = 0;
    }
    
    int get(int index) {
        LinkNode* cur = dummy;
        if(index>=size){
            return -1;
        }
        else{
            while(index>=0){
                cur = cur->next;
                index--;
            }
        return cur->val;
        }
    }
    
    void addAtHead(int val) {
        LinkNode* node = new LinkNode(val);
        node->next = dummy->next;
        dummy->next = node;
        size++;
    }
    
    void addAtTail(int val) {
        LinkNode* node = new LinkNode(val);
        LinkNode* cur = dummy;
        
        int l = size;
        while(l>0){
            cur = cur->next;
            l--;        
        }
        cur->next = node;
        node->next = nullptr;
        size++;
    }
    
    void addAtIndex(int index, int val) {
        if(index<=size){
            LinkNode* node = new LinkNode(val);
            LinkNode* cur = dummy;
            
            int l = index;
            while(l>0){
                cur = cur->next;
                l--;        
            }
            node->next = cur->next;
            cur->next = node;  
            size++;        

        }
    }
    
    void deleteAtIndex(int index) {
        if(index<size){
            LinkNode* cur = dummy;     
            int l = index;
            while(l>0){
                cur = cur->next;
                l--;        
            }
            cur->next = cur->next->next;
            size--;
        }
    }
private:
    int size;
    LinkNode* dummy;
};

206.反转链表

思路:设置两个指针依次反转当前节点和下一个节点的指向。

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* cur = head;

        if(head==nullptr||head->next==nullptr){
            return head;
        }

        ListNode* temp = cur->next;
        cur->next = nullptr;

        while(temp!=nullptr){
            ListNode* temp1 = temp->next;
            temp->next = cur;
            cur = temp;
            temp = temp1;                   
        }

        return cur;
    }
};

总结

注意链表的定义。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值