• c语言练习91:合并两个有序链表


    合并两个有序链表

    力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

    代码1:

    1. /**
    2. * Definition for singly-linked list.
    3. * struct ListNode {
    4. * int val;
    5. * struct ListNode *next;
    6. * };
    7. */
    8. typedef struct ListNode ListNode;
    9. struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){
    10. //判断链表是否为空
    11. if(list1==NULL){
    12. return list2;
    13. }
    14. if(list2==NULL){
    15. return list1;
    16. }
    17. //走到这里说明链表不为空,遍历链表
    18. ListNode*cur1=list1;
    19. ListNode*cur2=list2;
    20. ListNode*newhead,*newtail;
    21. newhead=newtail=NULL;
    22. while(cur1&&cur2){
    23. //1.空链表的情况下:插入的结点就是链表的头结点和尾结点
    24. //2.非空链表:插入的结点是链表的新的尾结点,头结点不变
    25. if(cur1->valval){
    26. if(newhead==NULL){
    27. newhead=newtail=cur1;
    28. }
    29. else{
    30. newtail->next=cur1;
    31. newtail=newtail->next;
    32. }
    33. cur1=cur1->next;
    34. }
    35. else{//cur2<=cur1
    36. if(newhead==NULL){
    37. newhead=newtail=cur2;
    38. }
    39. else{
    40. newtail->next=cur2;
    41. newtail=newtail->next;
    42. }
    43. cur2=cur2->next;
    44. }
    45. }
    46. if(cur1){
    47. newtail->next=cur1;
    48. }
    49. if(cur2){
    50. newtail->next=cur2;
    51. }
    52. return newhead;
    53. }

    优化:

    1. /**
    2. * Definition for singly-linked list.
    3. * struct ListNode {
    4. * int val;
    5. * struct ListNode *next;
    6. * };
    7. */
    8. typedef struct ListNode ListNode;
    9. struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){
    10. //判断链表是否为空
    11. if(list1==NULL){
    12. return list2;
    13. }
    14. if(list2==NULL){
    15. return list1;
    16. }
    17. //走到这里说明链表不为空,遍历链表
    18. ListNode*cur1=list1;
    19. ListNode*cur2=list2;
    20. ListNode*newhead,*newtail;//newhead为哨兵卫
    21. newhead=newtail=(ListNode*)malloc(sizeof(ListNode));
    22. // ListNode*newhead,*newtail;
    23. // newhead=newtail=NULL;
    24. while(cur1&&cur2){
    25. //1.空链表的情况下:插入的结点就是链表的头结点和尾结点
    26. //2.非空链表:插入的结点是链表的新的尾结点,头结点不变
    27. if(cur1->valval){
    28. // if(newhead==NULL){
    29. // newhead=newtail=cur1;
    30. // }
    31. // else{
    32. // newtail->next=cur1;
    33. // newtail=newtail->next;
    34. // }
    35. newtail->next=cur1;
    36. newtail=newtail->next;
    37. cur1=cur1->next;
    38. }
    39. else{
    40. //cur2<=cur1
    41. // if(newhead==NULL){
    42. // newhead=newtail=cur2;
    43. // }
    44. // else{
    45. // newtail->next=cur2;
    46. // newtail=newtail->next;
    47. // }
    48. newtail->next=cur2;
    49. newtail=newtail->next;
    50. cur2=cur2->next;
    51. }
    52. }
    53. if(cur1){
    54. newtail->next=cur1;
    55. }
    56. if(cur2){
    57. newtail->next=cur2;
    58. }
    59. ListNode*returnhead=newhead->next;
    60. free(newhead);
    61. return returnhead;
    62. }

    代码2:

    创建一个哨兵位。

    1. struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {
    2. if (list1 == NULL)
    3. return list2;
    4. if (list2 == NULL)
    5. return list1;
    6. struct ListNode *head =NULL, *tail = NULL;
    7. head = tail = (struct ListNode *)malloc(sizeof(struct ListNode));
    8. while (list1 && list2) {
    9. if (list1->val < list2->val) {
    10. tail->next = list1;
    11. tail = list1;
    12. list1 = list1->next;
    13. } else {
    14. tail->next = list2;
    15. tail = list2;
    16. list2 = list2->next;
    17. }
    18. }
    19. if (list1) {
    20. tail->next = list1;
    21. }
    22. if (list2) {
    23. tail->next = list2;
    24. }
    25. return head->next;
    26. }

    最后返回的应是哨兵位的下一个结点。

  • 相关阅读:
    数组与链表算法-单向链表算法
    python+vue+django医院健康体检预约系统697vf
    SMT:引领新时代公链赛道的龙头之选!
    04-学生选课(课程记录、订单创建、微信支付)
    暑期第一周总结
    Spring Boot中RedisTemplate的使用
    Python入门教程 | Python3 File(文件) 操作方法
    【Linux系统编程】
    Oneid方案
    AI编程助手 Amazon CodeWhisperer 全面解析与实践
  • 原文地址:https://blog.csdn.net/2301_77479435/article/details/133909828