(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)

相关推荐
LunarWave10 分钟前
12-指针和动态内存-malloc calloc realloc free
c语言·开发语言·c++
好好学习O(∩_∩)O14 分钟前
[Linux][进程] 进程终止
linux·开发语言·c++
杨荧26 分钟前
【JAVA开源】基于Vue和SpringBoot的周边产品销售网站
java·开发语言·vue.js·spring boot·spring cloud·开源
橘橙黄又青36 分钟前
多线程-初阶(1)
java·开发语言
.浓茶37 分钟前
c++11
开发语言·c++
宇卿.41 分钟前
Java中的while和do...while循环
java·开发语言
等什么君!43 分钟前
JavaScript数据类型
开发语言·前端·javascript
爱敲代码的小杨.1 小时前
【Python】2. 变量和数据类型
开发语言·python
NuyoahC1 小时前
算法笔记(七)——哈希表
c++·笔记·算法·哈希
kuai-1 小时前
MacOS配置python环境
开发语言·python·macos