• 【Leetcode】拿捏链表(一)——206.反转链表、203.移除链表元素


    作者:一个喜欢猫咪的的程序员

    专栏:《Leetcode》

    喜欢的话:世间因为少年的挺身而出,而更加瑰丽。                                  ——《人民日报》


    目录

    206.反转链表 

    203.移除链表元素


    206.反转链表 

    力扣https://leetcode.cn/problems/reverse-linked-list/题目描述:

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


     示例:

    示例 1:


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

     示例 2:

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

    示例 3:

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


    思路:

    思路一:头插法

    设一个变量cur遍历整个链表,并且创建一个新的链表rhead我们将cur一个一个头插进去,但这样会找不到cur的下一个位置,因此我们提前设一个变量next来找cur的下一个位置。

    时间复杂度:O(N)                                                            空间复杂度:O(1) 

    思路二:递归法

    利用递归找到最后一个位置,让他倒置

    需要注意的是 n1​ 的下一个节点必须指向空。如果忽略了这一点,链表中可能会产生环。

    时间复杂度:O(N)                                                            空间复杂度:O(N) 


    代码实现:

    思路一:头插法

    1. struct ListNode* reverseList(struct ListNode* head) {
    2. struct ListNode* cur = head;
    3. struct ListNode* rhead = NULL;
    4. while (cur)
    5. {
    6. struct ListNode* next = cur->next;
    7. cur->next = rhead;
    8. rhead = cur;
    9. cur = next;
    10. }
    11. return rhead;
    12. }

    思路二:

    1. struct ListNode* reverseList(struct ListNode* head){
    2. if (head == NULL || head->next == NULL) {
    3. return head;
    4. }
    5. struct ListNode*newhead=reverseList(head->next);
    6. head->next->next=head;
    7. head->next=NULL;
    8. return newhead;
    9. }

    203.移除链表元素

    力扣https://leetcode.cn/problems/remove-linked-list-elements/题目描述:

    给你一个链表的头节点 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
    输出:[]


    思路:

    思路一:尾插迭代法

    利用创建一个新的链表newhead,利用cur如果cur->val=val时,让这个cur下来到newhead,否则删掉。

     时间复杂度:O(N)                                                    空间复杂度:O(1)


    思路二:递归法

    对于给定的链表,首先对除了头节点 head以外的节点进行删除操作,然后判断 head 的节点值是否等于给定的 va。如果 head 的节点值等于va,则head 需要被删除,因此删除操作后的头节点为 head,next如果head的节点值不等于 val,则 head 保留,因此删除操作后的头节点还是 head。上述过程是一个递归的过程。

    递归的终止条件是 head 为空,此时直接返回 head。当 head 不为空时,递归地进行删除操作,然后判断 head 的节点值是否等于 val 并决定是否要删除head。

    作者:LeetCode-Solution
    来源:力扣(LeetCode)

    时间复杂度:O(N)                                                    空间复杂度:O(N)


    代码实现:

    思路一:尾插迭代法

    1. struct ListNode* removeElements(struct ListNode* head, int val) {
    2. struct ListNode* cur = head;
    3. struct ListNode* newhead, * tail;
    4. newhead = tail = NULL;
    5. if(head==NULL)
    6. {
    7. return NULL;
    8. }
    9. while (cur)
    10. {
    11. if (cur->val != val)
    12. {
    13. if (tail==NULL)
    14. {
    15. tail = newhead = cur;
    16. }
    17. else
    18. {
    19. tail->next = cur;
    20. tail = cur;
    21. }
    22. cur = cur->next;
    23. }
    24. else
    25. {
    26. struct ListNode* next = cur->next;
    27. free(cur);
    28. cur = next;
    29. }
    30. }
    31. if(tail!=NULL)
    32. tail->next = NULL;
    33. return newhead;
    34. }

    思路二:递归法:

    1. struct ListNode* removeElements(struct ListNode* head, int val) {
    2. if (head == NULL) {
    3. return head;
    4. }
    5. head->next = removeElements(head->next, val);
    6. return head->val == val ? head->next : head;
    7. }

  • 相关阅读:
    Python——弹幕词频统计及其文本分析(绘制词云)(含源代码)
    Rust 泛型
    【数据结构与算法】手撕二叉查找树
    Sentinel与OpenFeign 服务熔断那些事
    你想过如何让自己变得更好吗?-关于个人成长的思考二
    Java基于基于Springboot+vue的药品销售商城网站 在线购药 elementui毕业设计
    TreeMap和LinkedHashMap
    如何优雅的使用 GORM 来实现 MySQL 事务
    composer selfupdate或composer self-update不管用解决办法
    用Speedtest-Tracker跟踪上网速度
  • 原文地址:https://blog.csdn.net/m0_69061857/article/details/127766458