单链表合并2个已经按照该整数从小到大排好序的链表,合并后链表也是排好序的

typedef struct p
{
int data;
struct p *next;
}node;




node *uniteList(node *first ,node *second)
{
node *head = NULL;
node *cur = NULL;
node *temp= NULL;


while (first && second)
{
//利用值的大小,挨个遍历
if (first->data   < second->data)
{
temp = first;
first = first->next;
}
else
{
temp = second;
second= second->next;
}
if(head == NULL)
{
cur = temp;
head = cur;
}
else
{
cur->next = temp;
cur= temp;
}
}


if (first == NULL)
temp=second;
else
temp=first;


//挨个将每个结点插入一个新的链表
while (temp)
{
cur->next = temp;
temp=temp->next;
}
cur->next =NULL;
return head;
}

你可能感兴趣的:(单链表合并2个已经按照该整数从小到大排好序的链表,合并后链表也是排好序的)