| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1665 人关注过本帖
标题:请问这里为什么会超时
只看楼主 加入收藏
komorebi0110
Rank: 2
来 自:上海
等 级:论坛游民
帖 子:145
专家分:17
注 册:2019-11-23
结帖率:96.88%
收藏
已结贴  问题点数:20 回复次数:3 
请问这里为什么会超时
(有序表合并)给定有序的单链表La和Lb(元素类型为整型),请编写程序将La和Lb合并成为一个新的有序表。(注意:你的程序使用的辅助空间为常数,即若La中有m个节点,Lb中有n个节点,程序需要的节点空间为m+n+常数)
【输入】
单链表La的值(升序)(-1结束)
单链表Lb的值(升序)(-1结束)
【输出】
La和Lb排序的后的结果
例如:
【输入】
1 6 12 19 -1
3 7 16 29 45 99 -1
【输出】
1 3 6 7 12 16 19 29 45 99

#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
typedef int Node_entry;
struct Node {
 Node_entry entry;
 Node *next;
 Node( );
 Node(Node_entry item, Node *add_on = NULL);
}node;
Node::Node( )
{
 next=NULL;
}
Node::Node(Node_entry item,Node *add_on)
{
 entry=item;
 next=add_on;
}
Node* mergeTwoLists(Node* l1, Node* l2) {
      Node* preHead=new Node(-1);
        Node* prev=preHead;
        while (l1!=NULL&&l2!=NULL){
            if(l1->entry<l2->entry){
                prev->next=l1;
                l1=l1->next;
            }else{
                prev->next=l2;
                l2=l2->next;
            }
            prev=prev->next;
        }
        prev->next=l1==NULL?l2:l1;
        return preHead->next;
};
int main()
{
     int item;
     cin>>item;
     Node *head1=new Node(item);
     Node *p=head1;
     cin>>item;
     while(item!=-1)
     {
         p->next=new Node(item);
         p=p->next;
         cin>>item;
     }
     p=head1;
     getchar();
     delete p;
     p=NULL;
     cin>>item;
     Node *head2=new Node(item);
     p=head2;
     cin>>item;
     while(item!=-1)
     {
         p->next=new Node(item);
         p=p->next;
         cin>>item;
     }
     delete p;
     p=NULL;
     Node *q=mergeTwoLists(head1,head2);
     while(q)
    {
        printf("%d ",q->entry);
        q=q->next;
    }
}
搜索更多相关主题的帖子: NULL item next cin Node 
2020-05-05 16:01
lin5161678
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:45
帖 子:1136
专家分:3729
注 册:2011-12-3
收藏
得分:10 
     p=head1;
     getchar();
     delete p;
     p=NULL;

看不明白 你干嘛删除p?

https://zh.
2020-05-05 16:17
lin5161678
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:45
帖 子:1136
专家分:3729
注 册:2011-12-3
收藏
得分:10 
另外 空间复杂度不符合题目要求

https://zh.
2020-05-05 16:19
komorebi0110
Rank: 2
来 自:上海
等 级:论坛游民
帖 子:145
专家分:17
注 册:2019-11-23
收藏
得分:0 
谢谢!

我想要两颗西柚。
2020-05-06 13:40
快速回复:请问这里为什么会超时
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014922 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved