代码随想录第三天|203. 移除链表元素、707. 设计链表、206. 反转链表

203. 移除链表元素

思路:如题,考察链表,因为head也可能被移除,需要设置一下dummy_head,访问链表是通过指针,所以定义链表时记得使用ListNode*,访问内部元素使用->。Leetcode不需要自己定义链表,但是ACM模式就不一样了,所以要熟悉链表定义方式。
题解:

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

707. 设计链表

思路:依然考察链表,不难,倒是考察细心程度,记得定义_size方便剪枝。新增节点都用newnode命名,规范一点。
题解:

class MyLinkedList {
public:
    struct ListNode{
        int val;
        ListNode *next;
        ListNode(int x) : val(x),  next(nullptr){};
    };
    MyLinkedList() {
        _dummy_head = new ListNode(0);
        _size = 0;
    }
    
    int get(int index) {
        if (index >= _size || index < 0)
            return -1;
        ListNode *cur = _dummy_head -> next;
        while(index --)
            cur = cur -> next;
        return cur -> val;
    }
    
    void addAtHead(int val) {
        ListNode *newnode = new ListNode(val);
        newnode -> next = _dummy_head -> next;
        _dummy_head -> next = newnode;
        ++_size;
    }
    
    void addAtTail(int val) {
        ListNode *cur = _dummy_head;
        ListNode *newnode = new ListNode(val);
        while (cur -> next != nullptr)
            cur = cur -> next;
        cur -> next = newnode;
        ++_size; 
    }
    
    void addAtIndex(int index, int val) {
        ListNode *cur = _dummy_head;
        if (index > _size || index < 0)
            return;
        while (index --)
            cur = cur -> next;
        ListNode *newnode = new ListNode(val);
        newnode -> next = cur -> next;
        cur -> next = newnode;
        ++_size;
    }
    
    void deleteAtIndex(int index) {
        if (index >= _size || index < 0)
            return;
        ListNode *cur = _dummy_head;
        while (index --)
            cur = cur -> next;
        ListNode *tmp = cur -> next;
        cur -> next = cur -> next -> next;
        delete tmp;
        --_size;
    }
private:
    int _size;
    ListNode *_dummy_head;
};

206. 反转链表

思路:搞清楚要存什么就行。每遍历一个到节点,它需要连接到上一个节点,所以需要上一个节点的位置prev,连接完后需要跳转到原本的下一个节点tmp,然后更新prev的位置。
题解:

/**
 * 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* reverseList(ListNode* head) {
        ListNode *cur = head;
        ListNode *prev = nullptr;
        while (cur != nullptr)
        {
            ListNode *tmp = cur -> next;
            cur -> next = prev;
            prev = cur;
            cur = tmp;
        }
        return prev;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值