严晏来的博客

  • 关于我
  • 我的吉他作品
  • 我热爱的辩论
  • 我的技术博客
  • 我的软件作品
  • 我的小记
你好哇!欢迎来到我的博客!
在代码世界里摸爬滚打的菜鸟程序员一枚,喜欢搭系统、写代码,喜欢追寻各种技术,热爱生活,喜欢唱歌、弹吉他。
  1. 首页
  2. 我的技术博客
  3. leetcode刷题记录
  4. 正文

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

2024年8月20日 571点热度 0人点赞 0条评论

203.移除链表元素

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

总结:

  1. 对于指针类型的对象,必须使用 -> 来访问其成员;而对于非指针类型的对象,则需要使用.

  2. 注意判断条件不要只写cur != NULL,要写cur != NULL && cur->next != NULL

707.设计链表

大二刚开始学数据结构,第一节课就是写的这个。 期末考试也背了好久。不想再写了。

206.反转链表

/**
 * 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* reverseList(ListNode* head) {
        ListNode * cur = head;
        ListNode * pre = NULL;
        ListNode * temp;
        while(cur != NULL){
            temp = cur -> next;
            cur -> next = pre;
            pre =cur;
            cur = temp;
        }
        return pre;
    }
};

看懂了就没啥难度。

标签: 暂无
最后更新:2024年10月23日

YanYanlai

这个人很懒,什么都没留下

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

归档

  • 2024 年 11 月
  • 2024 年 10 月
  • 2024 年 8 月
  • 2024 年 5 月
  • 2023 年 6 月
  • 2023 年 3 月
  • 2022 年 12 月
  • 2022 年 9 月

分类

  • leetcode刷题记录
  • linux
  • web开发
  • 导航与定位
  • 我的小记
  • 我的技术博客
  • 我的软件作品
  • 数据结构与算法
  • 赵雷
  • 面试八股

COPYRIGHT © 2024 严晏来的博客. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang