(C++链表01) 移除链表元素

203、移除链表元素

不带头节点

cpp 复制代码
class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        while(head != NULL && head->val == val) {
            ListNode* tem = head;
            head = head->next;
            delete tem;
        }
        ListNode* cur = head;
        while(cur != NULL && cur->next != NULL) {
            if(cur->next->val == val) {
                ListNode* tem = cur->next;
                cur->next = tem -> next;
                delete tem;
            }else {
                cur = cur->next; 
            }
        }
        return head;
    }
};

时间复杂度:O(n)

空间复杂度:O(1)

带头节点

cpp 复制代码
class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        ListNode* dummy = new ListNode(0);
        dummy -> next = head;
        ListNode* cur = dummy;
        while(cur->next != NULL) {
            if(cur->next->val == val) {
                ListNode* tem = cur->next;
                cur->next = tem -> next;
                delete tem;
            }else {
                cur = cur->next; 
            }
        }
        return dummy->next;
    }
};

时间复杂度:O(n)

空间复杂度:O(1)

707、设计链表

注意链表结点的初始化、访问区间合不合法

cpp 复制代码
class MyLinkedList {
public:

    struct LinkedNode {
        int val;
        LinkedNode* next;
        LinkedNode(int val):val(val), next(nullptr){}
    };

    MyLinkedList() {
        dummyHead = new LinkedNode(0);
        size = 0;

    }
    
    int get(int index) {
        if(index <= -1 || index >= size) {
            return -1;
        }
        LinkedNode* cur = dummyHead->next;
        while(index--) {
            cur = cur->next;
        }
        return cur->val;
    }
    
    void addAtHead(int val) {
        LinkedNode* newNode = new LinkedNode(val);
        newNode->next = dummyHead->next;
        dummyHead->next = newNode;
        size++;
    }
    
    void addAtTail(int val) {
        LinkedNode* newNode = new LinkedNode(val);
        LinkedNode* cur = dummyHead;
        while(cur->next != NULL) {
            cur = cur->next;
        }
        cur->next = newNode;
        size++;
    }
    
    void addAtIndex(int index, int val) {
        if(index <= -1 || index > size) {
            return;
        }
        size++;
        if(index == size) {
            addAtTail(val);
            return;
        }
        LinkedNode* newNode = new LinkedNode(val);
        LinkedNode* cur = dummyHead;
        while(index--) {
            cur = cur->next;
        }
        newNode->next = cur->next;
        cur->next = newNode;
    }
    
    void deleteAtIndex(int index) {
        if(index <= -1 || index >= size) {
            return ;
        }
        LinkedNode* cur = dummyHead;
        while(index--) {
            cur = cur->next;
        }
        LinkedNode* tem = cur->next;
        cur->next = tem->next;
        delete tem;
        size--;
    }
    private: 
        LinkedNode* dummyHead;
        int size;
};

206、反转链表

双指针:

用cur指针遍历旧链表,head指针指向新链表的第一个结点,用tem指针取出遍历到的结点,将其指向head作为新链表的第一个节点,更新head。

cpp 复制代码
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* cur = head;
        head = NULL;
        ListNode* tem = NULL;
        while(cur != NULL) {
            tem = cur;
            cur = cur->next;
            tem-> next = head;
            head = tem;
        }
        return head;
    }
};

时间复杂度:O(n)

空间复杂度:O(1)

相关推荐
风与沙的较量丶26 分钟前
Java中的局部变量和成员变量在内存中的位置
java·开发语言
水煮庄周鱼鱼33 分钟前
C# 入门简介
开发语言·c#
澄澈天空37 分钟前
C++ MFC添加RichEditControl控件后,程序启动失败
c++·mfc
编程星空1 小时前
css主题色修改后会多出一个css吗?css怎么定义变量?
开发语言·后端·rust
软件黑马王子1 小时前
Unity游戏制作中的C#基础(6)方法和类的知识点深度剖析
开发语言·游戏·unity·c#
Lzc7741 小时前
C++初阶——简单实现vector
c++·简单实现vector
Logintern091 小时前
使用VS Code进行Python编程的一些快捷方式
开发语言·python
Multiple-ji2 小时前
想学python进来看看把
开发语言·python
一个小白12 小时前
C++——list模拟实现
开发语言·c++
bug总结2 小时前
新学一个JavaScript 的 classList API
开发语言·javascript·ecmascript