递归比较两个链表的头部
<span style="font-size:18px;">#include <iostream> #include <string.h> struct node { int data; node *next; }; node *MergeRecursive(node *head1, node *head2) { node *head = NULL; if (head1 == NULL) { return head2; } if (head2 == NULL) { return head1; } if (head1->data < head2->data) { head = head1 ; head->next = MergeRecursive(head1->next,head2); } else { head = head2 ; head->next = MergeRecursive(head1,head2->next); } return head ; } </span>
我采用的是递归方法,还有非递归的方法。