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

相关推荐
汉克老师4 分钟前
第十四届蓝桥杯青少组C++选拔赛[2023.2.12]第二部分编程题(1、求和)
c++·蓝桥杯·蓝桥杯c++·c++蓝桥杯
Source.Liu6 分钟前
【Python基础】 13 Rust 与 Python 注释对比笔记
开发语言·笔记·python·rust
qq_1955516918 分钟前
代码随想录70期day3
开发语言·python
XXYBMOOO23 分钟前
Qt UDP 通信类详解与实现
开发语言·网络·c++·qt·网络协议·ui·udp
pusue_the_sun28 分钟前
C语言强化训练(12)
c语言·开发语言·算法
君鼎28 分钟前
More Effective C++ 条款29:引用计数
c++
小欣加油32 分钟前
leetcode 6 Z字形变化
c++·算法·leetcode·职场和发展
曙曙学编程37 分钟前
stm32——寄存器操作,蜂鸣器原理
c语言·c++·stm32·单片机·嵌入式硬件
counting money1 小时前
JAVA泛型基础
java·开发语言·eclipse
田里的水稻1 小时前
C++_数据类型和数据结构
java·数据结构·c++