用JavaScript实现:
const link1 = new LinkedList() link1.append(1) link1.append(3) link1.append(4) link1.append(6) link1.append(7) const link2 = new LinkedList() link2.append(2) link2.append(5) link2.append(8) link2.append(9) function merginlist(list, otherlist) { //1、创建新链表 let newlist = new LinkedList(); let listHead = list.head let otherlistHead = otherlist.head; //2、比较 while (listHead && otherlistHead) { if (listHead.data < otherlistHead.data) { newlist.append(listHead.data) listHead = listHead.next } else { newlist.append(otherlistHead.data) otherlistHead = otherlistHead.next } } //3.list为空,另一个链表otherlist中的数据依次添加到新链表中 while (otherlistHead) { newlist.append(otherlistHead.data) otherlistHead = otherlistHead.next } // 3.otherlist为空,另一个链表list中的数据依次添加到新链表中 while (listHead) { newlist.append(listHead.data) listHead = listHead.next } return newlist.toString() } console.log(mergerlist(link1, link2));
标签:有序,otherlistHead,link2,两条,链表,listHead,link1,append From: https://www.cnblogs.com/LIXI-/p/16622668.html