• C\C++刷题ADY3


    题目来源:力扣

    1.第一题

    203. 移除链表元素 - 力扣(LeetCode)

    思路分析:(不带哨兵位的头节点)

    每次去分析一个节点,

    如果节点不是存的是6,就拿节点来尾插

    如果节点存的不是6,就把节点free掉

     思路分析:(带哨兵位的头节点)

    带哨兵位的头节点的优势之一就是方便尾插

    也就是说不需要判断开始尾插的时候是不是为NULL

    不带哨兵位头节点需要判断第一插入的时候的节点是不是NULL,以此区分后续的尾插

    PS:带哨兵位的思路请读者自己完成,尾插的思路并没有改变,变的仅仅是多了一个哨兵头节点,方便尾插

    带哨兵位的头节点仅仅只是方便尾插,其余的操作还是不带哨兵位的头节点的单链表更舒服,实际和OJ题目中几乎不用带哨兵位的头节点(除非明确说明)

    参考代码(不带哨兵位的头节点):

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

    2.第二题

    21. 合并两个有序链表 - 力扣(LeetCode)

     

     思路分析:

    给一个head指针

    给一个tail指针

    取较小的尾插

    参考代码1(不带哨兵位的头节点):

    1. struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2)
    2. {
    3. if(list1==NULL)
    4. return list2;
    5. if(list2==NULL)
    6. return list1;
    7. struct ListNode* head;
    8. struct ListNode* tail;
    9. head=tail=NULL;
    10. //取小的尾插
    11. while(list1 && list2)
    12. {
    13. if(list1->val < list2->val)
    14. {
    15. if(tail == NULL)
    16. {
    17. head = tail = list1;
    18. }
    19. else
    20. {
    21. tail->next = list1;
    22. tail = tail->next;
    23. }
    24. list1 = list1->next;
    25. }
    26. else
    27. {
    28. if(tail == NULL)
    29. {
    30. head = tail = list2;
    31. }
    32. else
    33. {
    34. tail->next = list2;
    35. tail = tail->next;
    36. }
    37. list2 = list2->next;
    38. }
    39. }
    40. if(list1)
    41. tail->next = list1;
    42. if(list2)
    43. tail->next = list2;
    44. return head;
    45. }

    参考代码2(带哨兵位的头节点):

    1. struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2)
    2. {
    3. struct ListNode*guard,*tail;
    4. guard = tail = (struct ListNode*)malloc(sizeof(struct ListNode));
    5. guard->next = NULL;
    6. //取小的尾插
    7. while(list1 && list2)
    8. {
    9. if(list1->valval)
    10. {
    11. tail->next = list1;
    12. tail = tail->next;
    13. list1 = list1->next;
    14. }
    15. else
    16. {
    17. tail->next = list2;
    18. tail = tail->next;
    19. list2 = list2->next;
    20. }
    21. }
    22. if(list1)
    23. {
    24. tail->next= list1;
    25. }
    26. if(list2)
    27. {
    28. tail->next=list2;
    29. }
    30. struct Node* head =guard->next;
    31. free(guard);
    32. return head;
    33. }

  • 相关阅读:
    不同路径数(冬季每日一题 4)
    luogu 2513 逆序对数列
    若依3.6.0使用Mybatis-plus分页失效以及完美替换Pagehelper
    我的Compose开源项目《出行防疫App》已发布
    C# redis通过stream实现消息队列以及ack机制
    关于WEB端实现电子海图研究二GeoServer
    倒计时2天,“文化数字化战略新型基础设施暨文化艺术链生态建设发布会”启幕在即
    Eclipse配置 eclipse Java 配置 eclipse 简单配置
    MyBatis--获取参数值
    window开机自动运行python脚本
  • 原文地址:https://blog.csdn.net/luoheng1114/article/details/127903866