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

203.移除链表元素

给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点 。

题目链接:203. 移除链表元素 - 力扣(LeetCode)

注意c++中NULL和nullptr的区别。应该用nullptr来表示空指针。

/**
 * 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* temp = new ListNode(0);
        temp->next = head;
        ListNode* a = temp;
        while (a->next != NULL) {
            if (a->next->val == val) {
                ListNode* a_next = a->next;
                a->next = a_next->next;
                delete a_next;            //注意,用delete不用free,且delete之后不能再使用此指针,故每次释放都需新建指针
            } else {
                a = a->next;
            }
        }
            return temp->next;
    }

}
;

 

707.设计链表

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

一坨石这个题

runtime error: member access within null pointer of type

此类报错是指试图使用空指针,注意哪里可能遇到空指针。

查看代码

 
class MyLinkedList {
public:
    MyLinkedList() {
        this->size = 0;
        this->head = new ListNode(0);
    }

    int get(int index) {
        if (index < 0 || index >= size) {
            return -1;
        }
        ListNode* cur = head;
        for (int i = 0; i <= index; i++) {
            cur = cur->next;
        }
        return cur->val;
    }

    void addAtHead(int val) { addAtIndex(0, val); }

    void addAtTail(int val) { addAtIndex(size, val); }

    void addAtIndex(int index, int val) {
        if (index > size)
            return;
        index = max(0, index);
        size++;
        ListNode* pred = head;
        for (int i = 0; i < index; i++) {
            pred = pred->next;
        }
        ListNode* toAdd = new ListNode(val);
        toAdd->next = pred->next;
        pred->next = toAdd;
    }

    void deleteAtIndex(int index) {
        if (index < 0 || index > size) {
            return;
        }
        size--;
        ListNode* pred = head;
        for (int i = 0; i < index; i++) {
            pred = pred->next;
        }
        ListNode* p = pred->next;
        pred->next = pred->next->next;
        delete p;
    }

private:
    int size;
    ListNode* head;
};

 206.反转链表 

给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。

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

不是很难,一遍过。

/**
 * 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 *temp=new ListNode(0);
        ListNode *temp_next=new ListNode(0);
        ListNode *r=new ListNode(0);
        temp->next=head;
        if(temp->next!=nullptr)
        temp_next=head->next;
        else
        return head;
        head->next=nullptr;
        while(temp_next!=nullptr){
            r=temp_next->next;
            temp_next->next=temp->next;
            temp->next=temp_next;
            temp_next=r;
        }
    return temp->next;
    }
};

下面是一个更优化的写法,思路一致:

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* cur = NULL, *pre = head;
        while (pre != NULL) {
            ListNode* t = pre->next;
            pre->next = cur;
            cur = pre;
            pre = t;
        }
        return cur;
    }
};

注意pre在右,cur在左

posted @ 2024-01-26 20:02  SandaiYoung  阅读(5)  评论(0编辑  收藏  举报