typedef struct LNode
{
int val;
struct LNode* next;
}ListNode;
ListNode* createNode(int val)
{
ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
if (newnode == NULL)
{
perror("error");
exit(1);
}
newnode->val = val;
newnode->next = NULL;
return newnode;
}
ListNode* create_LNode(ListNode* head, int val)
{
if (head == NULL)
{
ListNode* newnode = createNode(val);
head = newnode;
}
else
{
ListNode* newnode = createNode(val);
ListNode* pcur = head;
while (pcur->next)
{
pcur = pcur->next;
}
pcur->next = newnode;
}
return head;
}
ListNode* create_LA_LBnode(int LA[],int length)
{
ListNode* head = NULL;
int i;
for (i = 0; i < length; i++)
{
head = create_LNode(head, LA[i]);
}
return head;
}
ListNode* TWO_TO_ONE(ListNode* l1,ListNode*l2)
{
ListNode temp;
ListNode* pcur = &temp;
pcur->next = NULL;
while (l1 && l2)
{
if (l1->val <= l2->val)
{
pcur->next = l1;
l1 = l1->next;
}
else
{
pcur->next = l2;
l2 = l2->next;
}
pcur = pcur->next;
}
pcur->next = l1 ? l1 : l2;
return temp.next;
}
void printNode(ListNode* head)
{
ListNode* pcur = head;
while (pcur)
{
printf("%d->", pcur->val);
pcur = pcur->next;
}
printf("NULL\n");
}
int main()
{
int LA[] = { 3,5,9,11 };
int LB[] = { 2,4,6,8,10,12 };
ListNode* ListA = create_LA_LBnode(LA, sizeof(LA) / sizeof(LA[0]));
ListNode* ListB = create_LA_LBnode(LB, sizeof(LB) / sizeof(LB[0]));
printNode(ListA);
printNode(ListB);
ListNode*LA_LB = TWO_TO_ONE(ListA, ListB);
printNode(LA_LB);
return 0;
}
