鱼C论坛

 找回密码
 立即注册
查看: 1307|回复: 5

[已解决]C 两种解法觉得思路都正确,可只有一种可以通过

[复制链接]
发表于 2021-7-24 17:32:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 本空逐弥 于 2021-7-24 17:40 编辑
问题是将两个升序链表合并为一个新的升序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
示例:
输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4
/**
* Definition for singly-linked list.
* struct ListNode {
*     int val;
*     struct ListNode *next;
* };
*/
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2){
}

正确的代码如下
  1. struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2)
  2. {
  3.     if(l1==NULL) return l2;
  4.     if(l2==NULL) return l1;

  5.     struct ListNode *ph, *pn, *pl1, *pl2;
  6.     ph = NULL, pn = NULL, pl1 = l1, pl2 = l2;

  7.     (pl1->val < pl2->val)?(ph=pl1,pl1=pl1->next):(ph=pl2,pl2=pl2->next);

  8.     pn = ph;

  9.     while(1){
  10.         if(pl1 == NULL){
  11.             pn->next = pl2;
  12.             break;
  13.         }
  14.         if(pl2 == NULL){
  15.             pn->next = pl1;
  16.             break;
  17.         }
  18.         (pl1->val < pl2->val)?(pn->next=pl1,pl1 = pl1->next):(pn->next = pl2,pl2 = pl2->next);
  19.         pn = pn->next;
  20.     }

  21.     return ph;
  22. }
复制代码

不能通过的代码如下
  1. struct ListNode* mergeTwolists(ListNode* l1, ListNode* l2) {
  2.         ListNode *dummy ;
  3.         ListNode *cur;
  4.         dummy=(struct ListNode*)malloc(sizeof(struct ListNode);
  5.         cur=dummy;  
  6.         while (l1 != NULL && l2 != NULL) {
  7.             if (l1 -> val < l2 -> val) {
  8.                 cur -> next = l1;
  9.                 l1 = l1 -> next;
  10.             }
  11.             else {
  12.                 cur -> next = l2;
  13.                 l2 = l2 -> next;
  14.             }
  15.             cur = cur -> next;
  16.         }
  17.         cur -> next = (l1 != NULL ? l1 : l2);
  18.         return dummy -> next;
  19.     }
复制代码
觉得第二种简洁许多啊,怎么不能通过呢,输出没有反应。
求各位大佬指教~

最佳答案
2021-7-24 20:23:02
3 个问题
1. 不要用C++环境学习C语言
2. 申请了内存要释放
3. 写错单词

  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #define LEN sizeof(struct ListNode)

  4. struct ListNode
  5. {
  6.     int val;
  7.     struct ListNode *next;
  8. };

  9. int n;

  10. struct ListNode*creat()
  11. {
  12.     struct ListNode*head;
  13.     struct ListNode*p1,*p2;
  14.     n=0;
  15.     p1=p2=(struct ListNode*)malloc(LEN);
  16.     scanf("%d",&p1->val);
  17.     head=NULL;
  18.     while(p1->val!=0)
  19.     {
  20.         n=n+1;
  21.         if(n==1)head=p1;
  22.         else p2->next=p1;
  23.         p2=p1;
  24.         p1=(struct ListNode*)malloc(LEN);
  25.         scanf("%d",&p1->val);
  26.     }
  27.     p2->next=NULL;
  28.     return(head);
  29. }

  30. struct ListNode* mergeTwolists(struct ListNode* l1, struct ListNode* l2) {
  31.     struct ListNode *dummy ;
  32.     struct ListNode *cur;
  33.     dummy=(struct ListNode*)malloc(sizeof(struct ListNode));
  34.     cur=dummy;
  35.     while (l1 != NULL && l2 != NULL) {
  36.         if (l1 -> val < l2 -> val) {
  37.             cur -> next = l1;
  38.             l1 = l1 -> next;
  39.         }
  40.         else {
  41.             cur -> next = l2;
  42.             l2 = l2 -> next;
  43.         }
  44.         cur = cur -> next;
  45.     }
  46.     cur -> next = (l1 != NULL ? l1 : l2);
  47.     return dummy -> next;
  48. }


  49. void print(struct ListNode*head)
  50. {
  51.     struct ListNode*p;
  52.     p=head;
  53.     if(head!=NULL)
  54.         do
  55.         {
  56.             printf("%d ",p->val);
  57.             p=p->next;
  58.         }while(p!=NULL);
  59. }

  60. int main()
  61. {
  62.     struct ListNode *l1,*l2,*head;
  63.     l1=creat();
  64.     l2=creat();
  65.     //head=mergeTwoLists(l1,l2);
  66.     head = mergeTwolists(l1, l2);
  67.     print(head);
  68.     return 0;
  69. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-7-24 19:31:41 | 显示全部楼层
看起来好像是没问题,把全部的代码贴出来,我动态调试一下看看
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-7-24 19:32:47 | 显示全部楼层
人造人 发表于 2021-7-24 19:31
看起来好像是没问题,把全部的代码贴出来,我动态调试一下看看

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-7-24 19:34:09 | 显示全部楼层
人造人 发表于 2021-7-24 19:31
看起来好像是没问题,把全部的代码贴出来,我动态调试一下看看
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #define LEN sizeof(struct ListNode)

  4. struct ListNode
  5. {
  6.       int val;
  7.     struct ListNode *next;
  8. };
  9. int n;

  10. struct ListNode*creat()
  11. {
  12.     struct ListNode*head;
  13.     struct ListNode*p1,*p2;
  14.     n=0;
  15.     p1=p2=(struct ListNode*)malloc(LEN);
  16.     scanf("%d",&p1->val);
  17.     head=NULL;
  18.     while(p1->val!=0)
  19.     {
  20.         n=n+1;
  21.         if(n==1)head=p1;
  22.         else p2->next=p1;
  23.         p2=p1;
  24.         p1=(struct ListNode*)malloc(LEN);
  25.         scanf("%d",&p1->val);
  26.     }
  27.     p2->next=NULL;
  28.     return(head);
  29. }

  30. struct ListNode* mergeTwolists(ListNode* l1, ListNode* l2) {
  31.         ListNode *dummy ;
  32.         ListNode *cur;
  33.         dummy=(struct ListNode*)malloc(sizeof(struct ListNode);
  34.         cur=dummy;  
  35.         while (l1 != NULL && l2 != NULL) {
  36.             if (l1 -> val < l2 -> val) {
  37.                 cur -> next = l1;
  38.                 l1 = l1 -> next;
  39.             }
  40.             else {
  41.                 cur -> next = l2;
  42.                 l2 = l2 -> next;
  43.             }
  44.             cur = cur -> next;
  45.         }
  46.         cur -> next = (l1 != NULL ? l1 : l2);
  47.         return dummy -> next;
  48.     }


  49. void print(struct ListNode*head)
  50. {
  51.     struct ListNode*p;
  52.     p=head;
  53.     if(head!=NULL)
  54.         do
  55.     {
  56.         printf("%d ",p->val);
  57.         p=p->next;
  58.     }while(p!=NULL);
  59. }

  60. int main()
  61. {
  62.     struct ListNode *l1,*l2,*head;
  63.     l1=creat();
  64.     l2=creat();
  65.     head=mergeTwoLists(l1,l2);
  66.     print(head);
  67.         return 0;
  68. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-24 20:23:02 | 显示全部楼层    本楼为最佳答案   
3 个问题
1. 不要用C++环境学习C语言
2. 申请了内存要释放
3. 写错单词

  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #define LEN sizeof(struct ListNode)

  4. struct ListNode
  5. {
  6.     int val;
  7.     struct ListNode *next;
  8. };

  9. int n;

  10. struct ListNode*creat()
  11. {
  12.     struct ListNode*head;
  13.     struct ListNode*p1,*p2;
  14.     n=0;
  15.     p1=p2=(struct ListNode*)malloc(LEN);
  16.     scanf("%d",&p1->val);
  17.     head=NULL;
  18.     while(p1->val!=0)
  19.     {
  20.         n=n+1;
  21.         if(n==1)head=p1;
  22.         else p2->next=p1;
  23.         p2=p1;
  24.         p1=(struct ListNode*)malloc(LEN);
  25.         scanf("%d",&p1->val);
  26.     }
  27.     p2->next=NULL;
  28.     return(head);
  29. }

  30. struct ListNode* mergeTwolists(struct ListNode* l1, struct ListNode* l2) {
  31.     struct ListNode *dummy ;
  32.     struct ListNode *cur;
  33.     dummy=(struct ListNode*)malloc(sizeof(struct ListNode));
  34.     cur=dummy;
  35.     while (l1 != NULL && l2 != NULL) {
  36.         if (l1 -> val < l2 -> val) {
  37.             cur -> next = l1;
  38.             l1 = l1 -> next;
  39.         }
  40.         else {
  41.             cur -> next = l2;
  42.             l2 = l2 -> next;
  43.         }
  44.         cur = cur -> next;
  45.     }
  46.     cur -> next = (l1 != NULL ? l1 : l2);
  47.     return dummy -> next;
  48. }


  49. void print(struct ListNode*head)
  50. {
  51.     struct ListNode*p;
  52.     p=head;
  53.     if(head!=NULL)
  54.         do
  55.         {
  56.             printf("%d ",p->val);
  57.             p=p->next;
  58.         }while(p!=NULL);
  59. }

  60. int main()
  61. {
  62.     struct ListNode *l1,*l2,*head;
  63.     l1=creat();
  64.     l2=creat();
  65.     //head=mergeTwoLists(l1,l2);
  66.     head = mergeTwolists(l1, l2);
  67.     print(head);
  68.     return 0;
  69. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-25 00:45:14 From FishC Mobile | 显示全部楼层
两个问题,
第一,malloc 出来的 dummy 会返回一个没有数据,只有后继的头结点
第二,删除第二种写法的第15行
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-27 05:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表