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


203. 移除链表元素 - 力扣(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* removeElements(ListNode* head, int val) {
        ListNode* tmp;
        while(head!=nullptr&&head->val==val){
            tmp=head;
            head=head->next;
            delete tmp;
        }
        ListNode* cur=head;
        while(cur!=nullptr&&cur->next!=nullptr){
            if(cur->next->val==val){
                tmp=cur->next;
                cur->next=cur->next->next;
                delete tmp;
            }else{
                cur=cur->next;
            }
        }
        return head;
    }
};

707. 设计链表 - 力扣(LeetCode)

class MyLinkedList {
private:
    struct LinkNode {
        int val;
        LinkNode* next;
        LinkNode(int val):val(val), next(nullptr){}
    };
    LinkNode* head;
    int size=0;
public:
    MyLinkedList() {
        head=new LinkNode(0);
        size=0;
    }
    
    int get(int index) {
        if(index<0||index>=size){
            return -1;
        }
        LinkNode* tmp=head->next;
        for(int i=0;inext;
        }
        return tmp->val;
    }
    
    void addAtHead(int val) {
        LinkNode* tmp=new LinkNode(val);
        if(size==0){
            head->next=tmp;
        }else{
            tmp->next=head->next;
            head->next=tmp;
        }
        size++;
    }
    
    void addAtTail(int val) {
        LinkNode* tmp=new LinkNode(val);
        if(size==0){
            head->next=tmp;
        }else{
            LinkNode* cur=head->next;
            while(cur->next!=nullptr){
                cur=cur->next;
            }
            cur->next=tmp;
        }
        size++;
    }
    
    void addAtIndex(int index, int val) {
      if(index>size){
          return;
      }else if(index==size){
          addAtTail(val);
      }else if(index==0){
          addAtHead(val);
      }else{
          LinkNode* cur=head->next;
          for(int i=1;inext;
          }
        LinkNode* tmp=new LinkNode(val);
        tmp->next=cur->next;
        cur->next=tmp;
        size++;
      }
    }
    
    void deleteAtIndex(int index) {
        if (index<0||index >= size) {
            return;
        }
        LinkNode* cur=head;
        for(int i=0;inext;
        }
        LinkNode* tmp=cur->next;
        cur->next=cur->next->next;
        size--;
        delete tmp;
    }
};

/**
 * 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);
 */

/**
 * 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* newLinkedList=nullptr;
        ListNode* cur=head;
        while(cur!=nullptr){
            //头插法
            ListNode* tmp=cur->next;
            cur->next=newLinkedList;
            newLinkedList=cur;
            cur=tmp;
        }
        return newLinkedList;
    }
};
  • 在头插法中,应该先将 cur 的下一个节点存储起来,再将 cur 插入到新链表中。否则会出现指针丢失的问题。

你可能感兴趣的:(算法,leetcode,链表)