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

203.移除链表元素

题目

给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点

在这里插入图片描述
示例 1:

输入:head = [1,2,6,3,4,5,6], val = 6
输出:[1,2,3,4,5]

示例 2:

输入:head = [], val = 1
输出:[]

示例 3:

输入:head = [7,7,7,7], val = 7
输出:[]

解题思路

  • 链表增删改查节点都要会。
  • 用一个虚拟头节点指向头结点,操作完成之后,再删除虚拟头节点,这样简便。
  • 一定注意不要在修改节点指向的过程中,由于赋值操作的顺序导致节点丢失。
  • 用一个指针节点遍历链表,来删除指定元素。
  • 用完的节点记得删除。

代码

/**
 * 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 *fakeHead = new ListNode();
        fakeHead->next = head;
        ListNode *cur = fakeHead;
        while (cur->next != NULL) {
            if (cur->next->val == val) {
                ListNode *temp = cur->next;
                cur->next = cur->next->next;
                delete temp;
            } else {
                cur = cur->next;
            }
        }
        head = fakeHead->next;
        delete fakeHead;
        return head;
    }
};

707. 设计链表

题目

实现 MyLinkedList 类:

MyLinkedList() 初始化 MyLinkedList 对象。
int get(int index) 获取链表中下标为 index 的节点的值。如果下标无效,则返回 -1
void addAtHead(int val) 将一个值为 val 的节点插入到链表中第一个元素之前。在插入完成后,新节点会成为链表的第一个节点。
void addAtTail(int val) 将一个值为 val 的节点追加到链表中作为链表的最后一个元素。
void addAtIndex(int index, int val) 将一个值为 val 的节点插入到链表中下标为 index 的节点之前。如果 index 等于链表的长度,那么该节点会被追加到链表的末尾。如果 index 比长度更大,该节点将 不会插入 到链表中。
void deleteAtIndex(int index) 如果下标有效,则删除链表中下标为 index 的节点。

解题思路

  • 一道经典的自己手写链表的题,可以说是需要背会的一道题
  • 重点还是要深刻理解虚拟头节点的在整个链表中起的作用
  • 别忘了在每次增减操作之后,对链表的size 进行增减
  • 链表节点的下标是从0开始的,即第0各节点是头结点
  • 另外需要注意的一个点是,在get() addAtHead addAtIndex deleteAtIndex 时,cur节点指向的永远是待操作节点的前一个节点,也即 cur->next 指向的是目标节点。

代码


class MyLinkedList {
public:
    struct LinkedNode {
        int val;
        LinkedNode *nextNode;
        LinkedNode(int value) : val(value), nextNode(nullptr) {}
    };

    MyLinkedList() {
        _fakeHead = new LinkedNode(0);
        _size = 0;
    }

    int get(int index) {
        if (index < 0 || index > _size - 1) { return -1; }
        LinkedNode *cur = _fakeHead->nextNode;
        for (int i = 0; i < index; i++) { cur = cur->nextNode; }
        return cur->val;
    }

    void addAtHead(int val) {
        LinkedNode *newNode = new LinkedNode(val);
        newNode->nextNode = _fakeHead->nextNode;
        _fakeHead->nextNode = newNode;
        _size++;
    }

    void addAtTail(int val) {
        LinkedNode *newNode = new LinkedNode(val);
        LinkedNode *cur = _fakeHead->nextNode;
        while (cur->nextNode != nullptr) { cur = cur->nextNode; }
        cur->nextNode = newNode;
        _size++;
    }

    void addAtIndex(int index, int val) {
        if (index > _size) { return; }
        else if (index < 0) index = 0;
        LinkedNode *newNode = new LinkedNode(val);
        LinkedNode *cur = _fakeHead;
        for (int i = 0; i < index; ++i) { cur = cur->nextNode; }
        newNode->nextNode = cur->nextNode;
        cur->nextNode = newNode;
        _size++;

    }

    void deleteAtIndex(int index) {
        if (index >= _size || index < 0) { return; }
        LinkedNode *cur = _fakeHead;
        for (int i = 0; i < index; ++i) { cur = cur->nextNode; }
        LinkedNode *tmp = cur->nextNode;
        cur->nextNode = cur->nextNode->nextNode;
        delete tmp;
        //delete命令指示释放了tmp指针原本所指的那部分内存,
        //被delete后的指针tmp的值(地址)并非就是NULL,而是随机值。也就是被delete后,
        //如果不再加上一句tmp=nullptr,tmp会成为乱指的野指针
        //如果之后的程序不小心使用了tmp,会指向难以预想的内存空间
        tmp = nullptr;
        _size--;
    }

private:
    int _size;
    LinkedNode *_fakeHead;
};

206.反转链表

题目

给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。

示例 1:

输入:head = [1,2,3,4,5]
输出:[5,4,3,2,1]

示例 2:

输入:head = [1,2]
输出:[2,1]

示例 3:

输入:head = []
输出:[]

解题思路

  • 使用双指针的策略完成链表的翻转:cur 指向当前节点(从head节点开始循环)pre 指向 cur 的前一个节点
  • 循环使 cur->next 指向 pre 节点就完成了翻转
  • 在翻转过程中,还需要一个temp 指针,提前保存 cur 的下一个节点,防止在修改 cur->next 指向的时候,丢失了下一个节点
  • 空想比较容易出错,最好是画一下图就清晰了。

代码

链表节点结构体

class Solution {
public:
    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 *cur = head;
        ListNode *pre = nullptr;
        ListNode *temp;
        while (cur != nullptr) {
            temp = cur->next;
            cur->next = pre;
            pre = cur;
            cur = temp;
        }
        return pre;
    }
};

第二种:【递归

class Solution {
public:
    ListNode *reverseList(ListNode *head) {
        return reverse(nullptr, head);
    }
    ListNode *reverse(ListNode *pre, ListNode *cur) {
        if (cur == nullptr) return pre;
        ListNode *temp = cur->next;
        cur->next = pre;
        return reverse(cur, temp);	//这里的操作其实就是将cur当做参数传递给pre,temp当做参数传递给cur
    }
};
  • 8
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值