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())

 

posted on 2019-03-08 20:08  时光游弋  阅读(1200)  评论(0编辑  收藏  举报