代码随想录算法训练营第三天|203.移除链表元素,707.设计链表,206.反转链表。

203.移除链表元素

https://leetcode.cn/problems/remove-linked-list-elements/

这里就涉及如下链表操作的两种方式:

  • 直接使用原来的链表来进行删除操作。
  • 设置一个虚拟头结点在进行删除操作

<1>

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        while(head != NULL && head -> val == val){
            ListNode* tmp = head;
            head = head -> next;
            delete tmp;
        }
        ListNode* cur = head;
        while(cur != NULL && cur -> next != NULL){
            if(cur -> next ->val == val){
                ListNode* tmp = cur -> next;
                cur -> next = cur -> next -> next;
                delete tmp; 
            }else{
                cur = cur -> next;
            }
        }
        return head;
    }
};

用原来的链表需要单独写一段逻辑来处理移除头结点,定义tmp指针指向下一个后释放tmp。

<2>

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        ListNode* dummyHead = new ListNode(0);
        dummyHead -> next = head;
        ListNode* cur = dummyHead;
        while(cur -> next != nullptr){
            if(cur -> next -> val == val){
                ListNode* tmp = cur -> next;
                cur -> next = cur -> next -> next;
                delete tmp;
            }else{
                cur = cur -> next;
            }
        }

            head = dummyHead->next;
            delete dummyHead;
            return head;
    }
};

在链表增加一个虚拟的头节点,真正的头节点为 head = dummHead -> next

然后创建指针cur遍历,当cur指向的节点的值是val,让cur ->next 指向 cur ->next的 ->next即可

释放tmp的内存。最后返回真正的头节点并且释放dummyHead

707.设计链表

https://leetcode.cn/problems/design-linked-list/

这里设置一个虚拟的头节点让代码统一

class MyLinkedList {
public:
    struct LinkedNode{
        int val;
        LinkedNode* next;
        LinkedNode(int val):val(val), next(nullptr){}
    };
    MyLinkedList() {
        DummyHead = new LinkedNode(0);
        size = 0;
    }
    
    int get(int index) {
        if(index > (size - 1) || index < 0){
            return -1;
        }
        LinkedNode* cur = DummyHead -> next;
        while(index--){
            cur = cur -> next;
        }
        return cur -> val;
    }
    
    void addAtHead(int val) {
        LinkedNode* newNode = new LinkedNode(val);
        newNode -> next = DummyHead ->next ;
        DummyHead -> next = newNode;
        size++;
    }
    
    void addAtTail(int val) {
        LinkedNode* newNode = new LinkedNode(val);
        LinkedNode* cur = DummyHead;
        while(cur->next != nullptr){
            cur = cur->next;
        }
        cur -> next = newNode;
        size++;
    }
    
    void addAtIndex(int index, int val) {
        if(index > size) return;
        if(index < 0) index = 0;
        LinkedNode* newNode = new LinkedNode(val);
        LinkedNode* cur = DummyHead;
        while(index--){
            cur = cur->next;
        }
        newNode -> next = cur ->next;
        cur ->next = newNode;
        size++;
    }
    
    void deleteAtIndex(int index) {
        if (index >= size || index < 0) {
            return;
        }
        LinkedNode* cur = DummyHead;
        while(index--){
            cur = cur ->next;
        }
        LinkedNode* tmp = cur->next;
        cur->next = cur->next->next;
        delete tmp;
        tmp = nullptr;
        size--;

    }
    private:
    int size;
    LinkedNode* DummyHead;

};

addAtHead

 addAtTail

先遍历到最后一个,然后cur -> next = newNode; 即可。

addAtIndex

 deleteAtIndex

遍历到第Index的前一个后 ,让cur -> next = cur ->next ->next即可 ,然后释放cur ->next

206.反转链表

https://leetcode.cn/problems/reverse-linked-list/

<1>双指针

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

 当cur指向nullptr后,头节点不需要有人指向他,所以while的循环次数为cur,他指向空后就停止

先定义tmp保存下一个节点,然后翻转,再让cur和pre的值互换,最后更新新的cur节点。

<2>递归

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

与双指针思路一样,先初始化,递归相当于

pre = cur; cur = tmp;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值