首页 > 编程语言 >代码随想录算法训练营第一天| LeetCode203.移除链表元素、707. 设计链表、206.反转链表

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

时间:2022-12-31 13:22:37浏览次数:42  
标签:ListNode cur val int 随想录 next 链表 移除

203.移除链表元素

https://leetcode.cn/problems/remove-linked-list-elements/

struct ListNode{
    int val;
    ListNode* next;
    ListNode(){
        val=0;
        next=NULL;
    }
    ListNode(int x){
        val=x;
        next=NULL;
    }
    ListNode(int x,ListNode*next){
        val=x;this->next=next;
    }
    ListNode* removeElements(ListNode* head,int val){
        ListNode* shou=new ListNode();
        shou->next=head;
        ListNode*p=shou;
        while(p->next!=NULL){
            if(p->next->val==val){
                ListNode*temp=p->next;
                p->next=p->next->next;//p的next的next可能为空
                delete temp;
            }else p=p->next;
        }
        head=shou->next;
        delete shou;
        return head;
    }
};

 

707. 设计链表

https://leetcode.cn/problems/design-linked-list/

class MyLinkedList {
public:
    int size;
    struct listNode{
        int val;
        listNode* next;
        listNode(int val){
            this->val=val;
            this->next= nullptr;
        }
    };
    listNode* shou;
    MyLinkedList(){
        shou=new listNode(0);
        size=0;
    }
    int get(int index) {
        if(index>(size-1)||index<0)return -1;
        listNode* cur=shou->next;
        while(index--){
            cur=cur->next;
        }
        return cur->val;
    }

    void addAtHead(int val) {
        listNode *newhead=new listNode(val);
        newhead->next=shou->next;
        shou->next=newhead;
        size++;
    }

    void addAtTail(int val) {
        listNode* newnode=new listNode(val);
        listNode*cur=shou;
        while(cur->next!=NULL){//到达最后一个节点
            cur=cur->next;
        }
        cur->next=newnode;
        size++;
    }

    void addAtIndex(int index, int val) {
        if(index>size)return;
        if(index<0){
            index=0;
        }
        listNode* newnode=new listNode(val);
        listNode*cur=shou;
        while(index--){
            cur=cur->next;
        }
        newnode->next=cur->next;
        cur->next=newnode;
        size++;
    }

    void deleteAtIndex(int index) {
        if(index>size-1||index<0)return ;
        listNode* cur=shou;

        while(index--){
            cur=cur->next;
        }
        listNode* temp=cur->next;
        cur->next=cur->next->next;
        delete temp;
        size--;
    }
};

在此题中,除了插头结点外,其余都要重新声明一个新的指针变量cur避免影响shou成员的值从而让其他函数出错,注意,cur只是一个变量一个名字一个地址,当他被重新赋值不会改变整个链表的结构,可以想象为一个独立的节点。

206.反转链表

https://leetcode.cn/problems/reverse-linked-list/

这题我是利用双指针思想和链表知识点结合在一起。

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        int *arr=new int[5000];
        ListNode* cur=head;
        ListNode* frontcur=head;
        int i=0;
        while(cur!=NULL){
            arr[i++]=cur->val;
            cur=cur->next;
        }
        i--;
        while(frontcur!=NULL){
            frontcur->val=arr[i--];
            frontcur=frontcur->next;
        }
        return head;
    }
};

 

标签:ListNode,cur,val,int,随想录,next,链表,移除
From: https://www.cnblogs.com/zhishikele/p/17016486.html

相关文章

  • C/C++学生管理系统(单链表)[2022-12-31]
    C/C++学生管理系统(单链表)[2022-12-31]利用数据结构的单链表的框架实现学生管理系统以下功能要求:1)学生个人信息:姓名、学号、专业、性别、年龄、联系方式、成绩。2)学......
  • 代码随想录——回溯算法
    组合题目中等classSolution{List<List<Integer>>result=newArrayList<>();LinkedList<Integer>path=newLinkedList<>();publicList<List<Int......
  • 【迭代】【链表】206. 反转链表
    题目链接206.反转链表思路先添加一个头结点real_head。再设置real_head.next=null,通过不断运用头插法将p所指的结点插入到real_head.next,完成反转操作。代码class......
  • 代码随想录第三天 || 203.移除链表元素 || 707.设计链表 || 206.反转链表
    203.移除链表元素文章:代码随想录(programmercarl.com)视频:https://www.bilibili.com/video/BV18B4y1s7R9classSolution{public:ListNode*removeElements(Lis......
  • 代码随想录算法训练营第三天LeetCode203,707,206
    代码随想录算法训练营第三天|LeetCode203,707,206LeetCode203移除链表元素题目链接:https://leetcode.cn/problems/remove-linked-list-elements/description///区分了删......
  • BM5 合并k个已排序的链表
    题目描述合并k个升序的链表并将结果作为一个升序的链表返回其头节点。思路分析之前已经完成了两条有序链表的排序,那么对于任意条有序链表的合并我们都可以借助之前的......
  • BM7 链表中环的入口结点
    题目描述思路分析做这道题我第一反应是用“记事本”,也就是将遍历过的节点存储起来,如果下次再遍历到这个节点,那么也就是环的入口节点。遍历节点,如果是遍历过的,那么就直接......
  • 刷刷刷Day3| 203.移除链表元素 ,707.设计链表 ,206.反转链表
    203.移除链表元素LeetCode题目要求给你一个链表的头节点head和一个整数val,请你删除链表中所有满足Node.val==val的节点,并返回新的头节点。示例1输入:head=[......
  • 【归并排序】【链表】LeetCode 148. 排序链表
    题目链接148.排序链表思想分割cut环节:找到当前链表中点,并从中点将链表断开(以便在下次递归cut时,链表片段拥有正确边界)我们使用fast,slow快慢双指针法,奇数个......
  • 代码随想录算法训练营第三天
    学习新知识链表,刷题3道,先学习链表理论基础,然后刷题。 203.移除链表元素 707.设计链表 206.反转链表●  203.移除链表元素题目链接/文章讲解/视频讲解::https://......