0

i am trying to merg two linked list.

error occurs when i run the program it dead.

i believe it is because the function i defined to merge these two list has problems

would someone helpe me to fix it

or some hint would be nice

thanks

```
#include <stdio.h>
#include<malloc.h>
typedef struct listNode{
int* dataPtr;
struct listNode* next;
}ListNode;
typedef struct list{
ListNode* head;
ListNode* tail;
}List;
void initList(List *lst){
lst->head = NULL;
lst->tail = NULL;
}
void insertToEnd(List *lst, int *data){
ListNode *node;
node = (ListNode*)malloc(sizeof(ListNode));
node->dataPtr = data;
node->next = NULL;
if(lst->head == NULL){
lst->head = node;
}
else{
lst->tail->next = node;
}
lst->tail = node;
}
void add(List *lst, ListNode* node){
if(lst->head == NULL){
lst->head = node;
}
else{
lst->tail->next = node;
}
lst->tail = node;
}
List getNewList(){
List newList;
newList.head = NULL;
newList.tail = NULL;
return newList;
}
void printList(List *lst){
ListNode *node;
int *i;
node = lst->head;
while(node){
i = (node->dataPtr);
printf("%d ", *i);
node = node->next;
}
printf("\n");
}
List merge2(List lst1, List lst2){
ListNode *ptr1;
ListNode *ptr2;
ListNode *temp;
int *val1;
int *val2;
ptr1 = lst1.head;
ptr2 = lst2.head;
val1 = ptr1->dataPtr;
val2 = ptr2->dataPtr;
//swap ptr1 and ptr2 if ptr2 is smaller than ptr1
if(*val1 > *val2){
temp = lst2.head;
ptr2 = ptr2->next;
temp->next = lst1.head;
lst1.head = temp;
}
else {
ptr1 = ptr1->next;
}
while(ptr1 && ptr2){
val1 = ptr1->dataPtr;
val2 = ptr2->dataPtr;
if(*val1 > *val2){
temp->next = ptr2;
temp = ptr2;
ptr2 = ptr2-> next;
}
else {
ptr1 = ptr1->next;
}
if(ptr1 == NULL)
{ while(ptr2 != NULL)
temp->next=ptr2;
//add the rest of List2 to your current list
}
else if( ptr2 == NULL)
{
while(ptr1 != NULL)
temp->next=ptr1;
// add the rest of List1 to your current list.
}
}
return lst1;
}
int main(){
List lst1;
List lst2;
List mrg;
int i;
int nums1[4] = {2,3,9,10};
int nums2[3] = {0,2,6,};
initList(&lst1);
initList(&lst2);
for(i = 0 ;i < 4; i++){
insertToEnd(&lst1, (nums1 + i));
}
for(i = 0 ;i < 3; i++){
insertToEnd(&lst2, (nums2 + i));
}
mrg = merge2(lst1, lst2);
printList(&mrg);
return 0;
}
```