代码随想录算法03 | LC203、707、206

Leetcode203. 移除链表元素

题目链接:

[203. 移除链表元素 - 力扣(Leetcode)]

思路:

  • 简单的遍历遇见val删除即可
  • 注意链表的删除操作
  • c++最好要删除内存
class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        ListNode* dummyHead = new ListNode(0); // 设置一个虚拟头结点
        dummyHead->next = head; // 将虚拟头结点指向head,这样方面后面做删除操作
        ListNode* cur = dummyHead;
        while (cur->next != NULL) {
            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;
    }
};

Leetcode707. 设计链表

题目链接:

[707. 设计链表 - 力扣(Leetcode)]:

思路:

  • 考察链表的基本操作
  • 注意index的细节,根据题意设置cur是头结点还是head
class MyLinkedList {
public:
 struct LinkNode{
        int val;
        LinkNode *next;
        LinkNode(int val):val(val), next(nullptr){}
    };
    MyLinkedList() {
        headNode = new LinkNode(0);
        _size=0;
    }
    LinkNode *headNode;
    int _size;
    int get(int index) {
        if(index>(_size-1) ||index<0)
            return -1;
        LinkNode *cur =headNode->next;
        while(index--)
        {
            cur=cur->next;
        }    
        return cur->val;
    }
    
    void addAtHead(int val) {
        LinkNode *newNode = new LinkNode(val);
        newNode->next=headNode->next;
        headNode->next=newNode;
        _size++;
    }
    
    void addAtTail(int val) {
        LinkNode *newNode = new LinkNode(val);
        LinkNode *cur =headNode;
        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;
        LinkNode *newNode = new LinkNode(val);
        LinkNode *cur =headNode;
        while(index--)
        {
            cur=cur->next;
        }
        newNode->next=cur->next;
        cur->next=newNode;
        _size++;
    }
    
    void deleteAtIndex(int index) {
        if(index>=_size||index<0)
        {
           return ;
        }
        LinkNode *cur=headNode;
        while(index--)
        {
            cur=cur->next;
        }
        cur->next=cur->next->next;
        _size--;
    }

};

Leetcode206. 反转链表

题目链接:

[206. 反转链表 - 力扣(Leetcode)]

思路:

  • 类似头插法
  • 双指针从头至尾依次翻转
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode *cur=head;
        ListNode *pre=nullptr;
        ListNode *temp;
        while(cur!=nullptr)
        {
            temp=cur->next;
            cur->next=pre;
            pre=cur;
            cur=temp;
        }
        return pre;
    }
};

总结

遇到的问题:

  • LeetCode报错:runtime error: member access within null pointer of type 'struct ListNode'说明代码中可能使用空指针,多一步判断即可,排除对空指针的引用
  • 707设计链表多次报错,原因在于对index插入的前面后面顺序弄错了,根据题意设置cur指向头结点还是head

收获:

  • c++注意删除链表结点及时删除内存
  • 封装五个函数接口注意变量性质

你可能感兴趣的:(代码随想录,算法,链表,数据结构)