Coding:两个从大到小的有序链表合并成一个从小到大有序链表
生活随笔
收集整理的這篇文章主要介紹了
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;}總結
以上是生活随笔為你收集整理的Coding:两个从大到小的有序链表合并成一个从小到大有序链表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Coding: 一亿个数找最大的1000
- 下一篇: C++最简单的方式实现split分割函数