javascript 实现有序列表合并

1. 使用数组

 1         var A = [2, 4, 8, 11, 11, 15]
 2         var B = [3, 5, 6, 7, 9, 9, 19, 20]
 3         function mergeOrderedArr(A, B) {
 4             let i =0, j=0
 5             while (i < A.length && j < B.length) {
 6                 if (A[i] < B[j]) {
 7                     i++
 8                 } else {
 9                     A.splice(i, 0, B[j])
10                     i++
11                     j++
12                 }
13             }
14             if (j < B.length) {
15                 while (j < B.length) {
16                     A.push(B[j])
17                     j++
18                 }
19             }
20             return A
21         }
22         console.log(mergeOrderedArr(A, B))    

2. 链表

 1     function Node(ele) {
 2             this.element = ele
 3             this.next = null
 4         }
 5 
 6         function Link() {
 7             this.head = new Node('')
 8             this.appendNode = appendNode
 9             this.display = display
10         }
11 
12         function appendNode(ele) {
13             var newNode = new Node(ele)
14             var currentNode = this.head
15             if (this.head.next === null) {
16                 currentNode.next = newNode
17             } else {
18                 while (currentNode.next) {
19                     currentNode = currentNode.next
20                 }
21             }
22             currentNode.next = newNode
23         }       
24         
25         function display() {
26             var currentNode = this.head
27             let result = ''
28             while (currentNode.next) {
29                 currentNode = currentNode.next
30                 result += currentNode.next ? currentNode.element + ',' : currentNode.element
31             }
32             return result
33         }
34         var A = new Link()
35         A.appendNode(2)
36         A.appendNode(7)
37         A.appendNode(11)
38         A.appendNode(11)
39         A.appendNode(19)
40 
41         var B = new Link()
42         B.appendNode(3)
43         B.appendNode(5)
44         B.appendNode(10)
45         B.appendNode(13)
46         B.appendNode(17)
47         B.appendNode(21)
48         
49         var Ahead = A.head
50         var Bhead = B.head
51         var Apointer = Ahead.next
52         var Bpointer = Bhead.next
53         var Cpointer = Ahead
54         function merge(A, B) {
55 
56             while (Apointer && Bpointer) {
57                 if (Apointer.element > Bpointer.element) {
58                     Cpointer.next = Bpointer
59                     Cpointer = Bpointer
60                     Bpointer = Bpointer.next
61                 } else {
62                     Cpointer.next = Apointer
63                     Cpointer = Apointer
64                     Apointer = Apointer.next
65                 }
66             }
67             Cpointer.next = Apointer ? Apointer : Bpointer
68         }
69         merge(A, B)
70         console.log(A.display())

 

转载于:https://www.cnblogs.com/codejoker/p/10497956.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值