Skip to content

Commit

Permalink
数据结构之链表
Browse files Browse the repository at this point in the history
  • Loading branch information
mogu authored and mogu committed Jul 28, 2020
1 parent 115f42f commit 11562a2
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions 数据结构和算法/linklist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Node {
constructor(value) {
this.value = value
this.next = null
}
}

function createList(arr) {
let head = new Node(arr[0])
let temp = head
for (let i = 1; i < arr.length; i++) {
temp.next = new Node(arr[i])
temp = temp.next
}
return head
}

function reverseList(list) {
let arr = []
let head = list
let temp = head
console.log(temp)
while(temp) {
arr.push(temp.value)
temp = temp.next
}
console.log(arr)
temp = head

while(temp) {
temp.value = arr.pop()
temp = temp.next
}

return head
}

0 comments on commit 11562a2

Please sign in to comment.