merge two linked-list without sort
i write code that's merge two linked list without sort
I think it's little have lot of code have any idea for smaller code
sorry for my english
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
node * merage (node * list1, node * list2) {
// check witch list is empty
if (list1 == NULL) return list2;
if (list2 == NULL) return list1;
if ( list1 == NULL && list2 == NULL ) return NULL;
node * lstCpy = NULL;
node * head = NULL;
// copy list1 into lstCpy
node * cur = list1;
while (cur != NULL) {
if (head == NULL) {
lstCpy = new node;
head = lstCpy;
}
lstCpy->data = cur->data;
lstCpy->next = new node;
lstCpy = lstCpy->next;
cur = cur->next;
}
// copy list2 into lstCpy
cur = list2;
while (cur != NULL) {
lstCpy->data = cur->data;
if (cur->next != NULL)
lstCpy->next = new node;
lstCpy = lstCpy->next;
cur = cur->next;
}
delete lstCpy;
return head;
}
|
Last edited on
Post main code here and implementation of node
Topic archived. No new replies allowed.