add: 链表的中间节点
This commit is contained in:
17
test/list/ListNode.js
Normal file
17
test/list/ListNode.js
Normal file
@ -0,0 +1,17 @@
|
||||
export default class ListNode {
|
||||
constructor (val) {
|
||||
this.val = val
|
||||
this.next = null
|
||||
}
|
||||
}
|
||||
|
||||
export const arrToList = (arr) => {
|
||||
const head = new ListNode(arr[0])
|
||||
let current = head
|
||||
for (let n = 1, len = arr.length; n < len; n++) {
|
||||
current.next = new ListNode(arr[n])
|
||||
current = current.next
|
||||
}
|
||||
|
||||
return head
|
||||
}
|
Reference in New Issue
Block a user