struct Node
{
int data;
Node* next;
Node(int x): data(x), next(NULL){}
};
Node* fun1(Node* l1, Node* l2) {
Node* head = new Node(-1);
Node* ret = head;
while (l1 != NULL && l2 != NULL) {
if (l1->data < l2->data) {
head->next = l1;
l1 = l1->next;
} else {
head->next = l2;
l2 = l2->next;
}
head = head->next;
}
head->next = l1 == NULL ? l2 : l1;
return ret->next;
}
Node* fun2{Node* l1, Node* l2) {
if (l1 == NULL) return l2;
if (l2 == NULL) return l1;
if (l1->data > l2->data) {
l2->next = fun2(l1, l2->next);
return l2;
} else {
l1->next = fun2(l1->next, l2);
return l1;
}
}
合并两个有序链表 迭代和递归
猜你喜欢
转载自blog.csdn.net/qq_24624539/article/details/108782976
今日推荐
周排行