【代码随想录|203.移动链表元素、707.设计链表、206.翻转链表】

203.移动链表元素

题目链接:

203. 移除链表元素 - 力扣(LeetCode)

为什么不用cur->val直接删除,因为要用前一个节点指向后一个节点,你直接找cur->val找不到前一个节点,所以要用cur->next->val==val才行

最后返回节点不是直接返回head,因为因为头结点head可能也被删除了,所以获取头结点要用dummyhead的下一位来获取707

/**
 * 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(0);
        dummyHead->next = head;
        ListNode* cur = dummyHead;
        while (cur->next != NULL){
            if (cur->next->val == val){
                ListNode* temp = cur->next;
                cur->next = cur->next->next; 
                delete temp; 
            }
            else {
                cur=cur->next; 
            }
        }
        head = dummyHead->next;
        delete dummyHead;
        return head;
    }
};

707.设计链表

题目链接:707. 设计链表 - 力扣(LeetCode)

class MyLinkedList {
public:
    struct LinkedList {
        LinkedList* next;
        int val;
        LinkedList(int val) : val(val),next(nullptr){};
    };
    MyLinkedList() {
        dummyHead = new LinkedList(0);
        size = 0;
    }

    int get(int index) {
        if (index >= size || index < 0)
            return -1;
        LinkedList* cur = dummyHead->next;
        while (index--) {
            cur = cur->next;
        }
        return cur->val;
    }

    void addAtHead(int val) {
        LinkedList* newNode = new LinkedList(val);
        newNode->next = dummyHead->next;
        dummyHead->next = newNode;
        size++;
    }

    void addAtTail(int val) { 
        LinkedList* newNode = new LinkedList(val); 
        LinkedList* 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;
        LinkedList* newNode = new LinkedList(val);
        LinkedList* cur = dummyHead;
        while (index--){
            cur = cur->next;
        }
        newNode->next = cur->next;
        cur->next = newNode;
    }

    void deleteAtIndex(int index) {
        if (index >= size || index < 0)
        return;
        LinkedList* cur = dummyHead;
        while (index--){
            cur = cur->next;
        }
        LinkedList* tmp = cur->next;
        cur->next = cur->next->next;
        delete tmp;
        tmp = nullptr;
    }

private:
    LinkedList* dummyHead;
    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);
 */

 就是在赋值的时候,因为头结点是大家公用的,所以要直接

dummyHead = new LinkedList(0);
size = 0;

如果在前面加了前缀,那就不是公用的那个dummyHead了

206.翻转链表

题目链接:206. 反转链表 - 力扣(LeetCode)

//双指针法
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* pre = nullptr;
        ListNode* cur = head;
        ListNode* tmp;
        while(cur){
            tmp = cur->next;
            cur->next = pre;
            pre = cur;
            cur = tmp;
        }
        return pre;
    }
};
//递归法
class Solution {
public:
    ListNode* reverse(ListNode* pre, ListNode* cur){
        if(cur == nullptr) return pre;
        ListNode* tmp = cur->next;
        cur->next = pre;
        return reverse(cur, tmp);
    }
    ListNode* reverseList(ListNode* head) {
        return reverse(nullptr, head);
    }
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值