/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
struct ListNode
{
int val;
struct ListNode *next;
};
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2)
{
struct ListNode answer;
struct ListNode *ans = &answer;
if(list1 == NULL && list2 == NULL)
return NULL;
while(list1 && list2)
{
if((list1 -> val) < (list2 -> val))
{
ans -> next = list1;
list1 = list1 -> next;
ans = ans -> next;
}
else
{
ans -> next = list2;
list2 = list2 -> next;
ans = ans -> next;
}
}
if (list1) {
ans -> next = list1;
}
if (list2) {
ans -> next = list2;
}
return answer.next;
}