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

203.移除链表元素

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

struct ListNode{
    int val;
    ListNode* next;
    ListNode(){
        val=0;
        next=NULL;
    }
    ListNode(int x){
        val=x;
        next=NULL;
    }
    ListNode(int x,ListNode*next){
        val=x;this->next=next;
    }
    ListNode* removeElements(ListNode* head,int val){
        ListNode* prehead=new ListNode();
        prehead->next=head;
        ListNode*p=shou;
        while(p->next!=NULL){
            if(p->next->val==val){
                ListNode*temp=p->next;
                p->next=p->next->next;//p的next的next可能为空
                delete temp;
            }else p=p->next;
        }
        head=prehead->next;
        delete prehead;
        return head;
    }
};

 第二种写法

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        ListNode* prehead=new ListNode;
        prehead->next=head;
        ListNode* cur=prehead;
        while(cur!=NULL){
           while(cur->next!=nullptr&&cur->next->val==val){//注意&&前后不能颠倒还有不能用if
             ListNode* temp=cur->next;
             cur->next=cur->next->next;
             delete temp;
          }
          cur=cur->next;
        }
        head=prehead->next;
        delete prehead;
        return head;
    }
};

 

707. 设计链表

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

class MyLinkedList {
public:
    int size;
    struct listNode{
        int val;
        listNode* next;
        listNode(int val){
            this->val=val;
            this->next= nullptr;
        }
    };
    listNode* shou;
    MyLinkedList(){
        shou=new listNode(0);
        size=0;
    }
    int get(int index) {
        if(index>(size-1)||index<0)return -1;
        listNode* cur=shou->next;
        while(index--){
            cur=cur->next;
        }
        return cur->val;
    }

    void addAtHead(int val) {
        listNode *newhead=new listNode(val);
        newhead->next=shou->next;
        shou->next=newhead;
        size++;
    }

    void addAtTail(int val) {
        listNode* newnode=new listNode(val);
        listNode*cur=shou;
        while(cur->next!=NULL){//到达最后一个节点
            cur=cur->next;
        }
        cur->next=newnode;
        size++;
    }

    void addAtIndex(int index, int val) {
        if(index>size)return;
        if(index<0){
            index=0;
        }
        listNode* newnode=new listNode(val);
        listNode*cur=shou;
        while(index--){
            cur=cur->next;
        }
        newnode->next=cur->next;
        cur->next=newnode;
        size++;
    }

    void deleteAtIndex(int index) {
        if(index>size-1||index<0)return ;
        listNode* cur=shou;

        while(index--){
            cur=cur->next;
        }
        listNode* temp=cur->next;
        cur->next=cur->next->next;
        delete temp;
        size--;
    }
};

在此题中,除了插头结点外,其余都要重新声明一个新的指针变量cur避免影响shou成员的值从而让其他函数出错,注意,cur只是一个变量一个名字一个地址,当他被重新赋值不会改变整个链表的结构,可以想象为一个独立的节点。

206.反转链表

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

这题我是利用双指针思想和链表知识点结合在一起。但空间复杂度高。下面是更好的方法,将双指针思想更好地利用到了

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        int *arr=new int[5000];
        ListNode* cur=head;
        ListNode* frontcur=head;
        int i=0;
        while(cur!=NULL){
            arr[i++]=cur->val;
            cur=cur->next;
        }
        i--;
        while(frontcur!=NULL){
            frontcur->val=arr[i--];
            frontcur=frontcur->next;
        }
        return head;
    }
};

 更好的方法:

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* temp; // 保存cur的下一个节点
        ListNode* cur = head;
        ListNode* pre = NULL;
        while(cur) {
            temp = cur->next;  // 更新temp
            cur->next = pre; 
            // 更新pre 和 cur指针
            pre = cur;
            cur = temp;
        }
        return pre;
    }
};

 

posted @ 2022-12-31 13:13  芝士可乐  阅读(191)  评论(0)    收藏  举报