
合并链表-DirectX 12编程入门(龙书)
5星
- 浏览量: 0
- 大小:None
- 文件类型:PDF
简介:
(2)合并链表
The process of reversing the linked list has already been completed.
Could you clarify what you are asking for regarding merging?
Should the merged result maintain a sorted order?
I am restricted to performing merges that maintain order.
Node * merge(Node * h1, Node * h2) {
if (h1 == NULL) return h2;
if (h2 == NULL) return h1;
Node * head;
if (h1->data > h2->data) {
head = h2;
h2 = h2->next;
} else {
head = h1;
h1 = h1->next;
}
Node * current = head;
while (h1 != NULL && h2 != NULL) {
if (h1 == NULL || (h2!=NULL && h1->data > h2->data)) {
current->next = h2;
h2 = h2->next;
current = current->next;
} else {
current->next = h1;
h1 = h1->next;
current = current->next;
}
}
current->next = NULL;
return head;
The function has been implemented to merge two sorted linked lists efficiently.
全部评论 (0)


