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

203加一个头节点统一进行操作,很方便,最后提交的时候去掉头节点就好。

复制代码
 1 class Solution {
 2 public:
 3     ListNode* removeElements(ListNode* head, int val) {
 4         //为了操作统一 还是整一个头出来比较合适
 5         //依旧是多一个头 好操作
 6         ListNode* h = new ListNode(0);
 7         h->next = head;
 8         ListNode* h1 = h;
 9         while(h->next != NULL)
10         {
11             if(h->next->val == val)
12             {
13                 h->next = h->next->next;
14             }
15             else{
16                 h = h->next;  
17             }
18         }
19         return h1->next;
20     }
21 };
复制代码

707就是考察链表的基础操作

复制代码
  1 class MyLinkedList
  2 {
  3 public:
  4 
  5     MyLinkedList()//初始化链对象
  6     {
  7         ListNode* head1 = new ListNode(4396);
  8         head1->next = NULL;
  9         len = 0;//带头结点 所有第一个不算入长度
 10         //这个值我带不回来
 11         head = head1;
 12     }
 13 
 14     void Print() {//打印整个链表
 15         t = head;
 16         while (t) {
 17             cout << t->val << " ";
 18             t = t->next;
 19         }
 20         cout << endl;
 21     }
 22 
 23     int get(int index)//获取链表中下标为index的节点值
 24     {
 25         if (index >= len || index < 0)
 26         {
 27             return -1;
 28         }
 29         t = head;
 30         while (index-- >= 0) {
 31             t = t->next;
 32         }
 33         return t->val;
 34     }
 35 
 36     void addAtHead(int val)//头插入
 37     {
 38         t = new ListNode(val);
 39         t->next = head->next;
 40         head->next = t;
 41         len++;
 42     }
 43 
 44     void addAtTail(int val)//尾插入
 45     {
 46         t = head;
 47         while (t->next)
 48         {
 49             t = t->next;
 50         }
 51         t->next = new ListNode(val);
 52         len++;
 53     }
 54 
 55     void addAtIndex(int index, int val)
 56     {
 57         if (index > len)
 58         {
 59             return;
 60         }
 61         t = head;
 62         while (index-- > 0)
 63         {
 64             t = t->next;
 65         }
 66         ListNode* temp = new ListNode(val);
 67         temp->next = t->next;
 68         t->next = temp;
 69         len++;
 70     }
 71 
 72     void deleteAtIndex(int index)
 73     {
 74         if (index < 0 || index >= len) 
 75         {
 76             return;
 77         }
 78         t = head;
 79         while (index-- > 0)
 80         {
 81             t = t->next;
 82         }
 83         t->next = t->next->next;
 84         len--;
 85     }
 86 
 87 private:
 88     struct ListNode {
 89         int val;
 90         ListNode* next;
 91         //结构体内可以塞构造函数 而且参数列表不同 可以重载
 92         ListNode(int x)
 93         {
 94             val = x;
 95             next = NULL;
 96         }
 97         //或者写简略形式
 98         //ListNode(int x):val(x),next(NULL){}
 99     };
100     ListNode* head;//头节点
101     ListNode* t;//用于类中函数使用
102     int len;//链表长度
103 };
复制代码

206题把原来的链表当输入材料,重新输入一遍即可。

复制代码
 1 class Solution {
 2 public:
 3     //将原链表当作数据来源 重新以头插法做表
 4     ListNode* reverseList(ListNode* head) {
 5         ListNode* ans = new ListNode(0);
 6         ListNode* h = head;
 7         while(h){
 8             h = h->next;
 9             head->next = ans->next;
10             ans->next = head;
11             head = h;
12         }
13         return ans->next;
14     }
15 };
复制代码

 

posted @   清源风起时  阅读(708)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示