Coding:两个从大到小的有序链表合并成一个从小到大有序链表
要求
请实现一个函数,把两个从大到小的有序链表合并成一个链表,新的链表是一个从小到大的有序链表。
struct list
{int value;list* next;
};
list * merge (list *list1_head, list*list2_head);
代码
list *merge(list *list1_head,list *list2_head){list *newlist = NULL;list *current = NULL;while(NULL!=list1_head && NULL!=list2_head){if(list1_head->value > list2_head->value){current = list1_head->next;list1_head->next = newlist;newlist = list1_head;list1_head = current;}else{current = list2_head->next;list2_head->next = newlist;newlist = list2_head;list2_head = current;}}while(NULL!=list1_head){current = list1_head->next;list1_head->next = newlist;newlist = list1_head;list1_head = current;}while(NULL!=list2_head){current = list2_head->next;list2_head->next = newlist;newlist = list2_head;list2_head = current;}return newlist;}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
