您的位置:首页 > 其它

合并俩个排序的链表

2015-01-27 11:11 106 查看
1.问题描述

输入俩个递增排序的链表,合并这俩个链表并使新链表中的结点仍然递增有序。(来自《剑指offer》)

2.代码

typedef struct node
{
struct node* next;

int data;

}ListNode;

ListNode* MerageList(ListNode* one, ListNode* two)
{
if (one == NULL)
{
return  two;
}

if (two == NULL)
{
return  one;
}

ListNode * head;

ListNode *node  = head;

while (one != NULL && two != NULL)
{
if (one->data < two->data)
{
node->next = one;

one = one->next;
}
else
{
node->next = two;

two= two ->next;
}

node = node->next;

}

if (two != NULL)
{
node->next = two;
}

if(one != NULL)
{
node->next = one;
}

return head;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: