(1)new乙個新的鍊錶,定義結點用來遍歷兩個待排序的鍊錶;
(2)兩個待排序的鍊錶,哪個頭結點小,哪個作為新鍊錶的頭結點;
(3)對兩個鍊錶的結點進行比較,讓新鍊錶的next指向較小的結點,更新新鍊錶的尾巴,繼續比較直至某個鍊錶遍歷結束。
(4)遍歷結束,如果哪個鍊錶沒遍歷完,將剩下的幾點串在新鍊錶的後面,即當前尾巴的next指向為遍歷完的鍊錶。
public class mergelink else if (link2.size() == 0) else if (link1.size() == 0 && link2.size() == 0)
singlelinknewsinglelink = new singlelink<>();
singlelink.entryl1 = link1.getheadentry();
singlelink.entryl2 = link2.getheadentry();
if(l1.getvalue().compareto(l2.getvalue()) < 0)else
while(l1 != null && l2 != null)else
}if(l1 == null)
if(l2 == null)
return newsinglelink;
}public static void main(string args)
}
3 兩個有序單鏈表合併為乙個有序的單鏈表
單鏈表的基本操作 包括 鍊錶的建立 頭插法,尾插法 鍊錶的遍歷,兩個單調遞增的鍊錶合併為乙個單調遞增的鍊錶,兩個單調遞增的鍊錶合併為乙個單調遞減的鍊錶 include include using namespace std 定義單鏈表的接點 typedef struct nodenodes 將兩個單...
兩個有序鍊錶合併為有序
include 兩個有序遞增的單鏈表 有頭結點 合併為新單鏈表。include 用尾插法表示這兩個鍊錶較好,因為這樣建立鍊錶是有序的,遞增的。如果用頭插法,for迴圈改為for i n i 0,i 使其新建的鍊錶遞增 typedef struct lnode lnode lnode creat ln...
兩個鍊錶合併為乙個鍊錶
include include typedef struct node node typedef struct node linklist using namespace std void creat linklist l,int n cout the linklist is full next n...