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
|
||||
}
|
@ -1,20 +1,5 @@
|
||||
import { hasCycle } from '../../src/list/linked-list-cycle'
|
||||
|
||||
function ListNode (val) {
|
||||
this.val = val
|
||||
this.next = null
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
import { arrToList } from './ListNode'
|
||||
|
||||
test('环形链表', () => {
|
||||
const list = arrToList([1, 2, 3])
|
||||
|
@ -1,62 +1,45 @@
|
||||
import { merge, mergeKLists } from '../../src/list/merge-k-sorted-lists'
|
||||
|
||||
function ListNode (val) {
|
||||
this.val = val
|
||||
this.next = null
|
||||
}
|
||||
|
||||
const arr2List = (arr) => {
|
||||
let first = null
|
||||
let res = null
|
||||
for (let n = 0, len = arr.length; n < len; n++) {
|
||||
const tmp = new ListNode(arr[n])
|
||||
if (res) res.next = tmp
|
||||
else first = tmp
|
||||
res = tmp
|
||||
}
|
||||
|
||||
return first
|
||||
}
|
||||
import { arrToList } from './ListNode'
|
||||
|
||||
test('数组转链表', () => {
|
||||
expect(arr2List([1, 2, 3])).toEqual({ next: { next: { next: null, val: 3 }, val: 2 }, val: 1 })
|
||||
expect(arrToList([1, 2, 3])).toEqual({ next: { next: { next: null, val: 3 }, val: 2 }, val: 1 })
|
||||
})
|
||||
|
||||
test('合并俩链表', () => {
|
||||
const source1 = arr2List([1, 4, 5])
|
||||
const source1 = arrToList([1, 4, 5])
|
||||
|
||||
const source2 = arr2List([1, 3, 4])
|
||||
const source2 = arrToList([1, 3, 4])
|
||||
|
||||
const out = [1, 1, 3, 4, 4, 5]
|
||||
expect(merge(source1, source2)).toEqual(arr2List(out))
|
||||
expect(merge(source1, source2)).toEqual(arrToList(out))
|
||||
})
|
||||
|
||||
test('合并K个排序链表 - 1', () => {
|
||||
const source = [
|
||||
arr2List([1, 4, 5]),
|
||||
arr2List([1, 3, 4]),
|
||||
arr2List([2, 6])
|
||||
arrToList([1, 4, 5]),
|
||||
arrToList([1, 3, 4]),
|
||||
arrToList([2, 6])
|
||||
]
|
||||
const out = [1, 1, 2, 3, 4, 4, 5, 6]
|
||||
expect(mergeKLists(source)).toEqual(arr2List(out))
|
||||
expect(mergeKLists(source)).toEqual(arrToList(out))
|
||||
})
|
||||
|
||||
test('合并K个排序链表 - 2', () => {
|
||||
const source = [
|
||||
arr2List([2, 4, null]),
|
||||
arr2List([null]),
|
||||
arr2List([-1, null])
|
||||
arrToList([2, 4, null]),
|
||||
arrToList([null]),
|
||||
arrToList([-1, null])
|
||||
]
|
||||
const out = [-1, 2, 4, null]
|
||||
expect(mergeKLists(source)).toEqual(arr2List(out))
|
||||
expect(mergeKLists(source)).toEqual(arrToList(out))
|
||||
})
|
||||
|
||||
test('合并K个排序链表 - 3', () => {
|
||||
const source = [
|
||||
arr2List([2, 6, null]),
|
||||
arr2List([5, null]),
|
||||
arr2List([7, null])
|
||||
arrToList([2, 6, null]),
|
||||
arrToList([5, null]),
|
||||
arrToList([7, null])
|
||||
]
|
||||
const out = [2, 5, 6, 7, null]
|
||||
expect(mergeKLists(source)).toEqual(arr2List(out))
|
||||
expect(mergeKLists(source)).toEqual(arrToList(out))
|
||||
})
|
||||
|
@ -1,20 +1,5 @@
|
||||
import { mergeTwoLists } from '../../src/list/merge-two-sorted-lists'
|
||||
|
||||
function ListNode (val) {
|
||||
this.val = val
|
||||
this.next = null
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
import { arrToList } from './ListNode'
|
||||
|
||||
test('合并两个有序链表', () => {
|
||||
expect(mergeTwoLists(arrToList([1, 2, 4]), arrToList([1, 3, 4]))).toEqual(arrToList([1, 1, 2, 3, 4, 4]))
|
||||
|
9
test/list/middle-of-the-linked-list.test.js
Normal file
9
test/list/middle-of-the-linked-list.test.js
Normal file
@ -0,0 +1,9 @@
|
||||
import { middleNode } from '../../src/list/middle-of-the-linked-list'
|
||||
import { arrToList } from './ListNode'
|
||||
|
||||
test('链表的中间节点', () => {
|
||||
expect(middleNode(arrToList([1]))).toEqual(arrToList([1]))
|
||||
expect(middleNode(arrToList([1, 2]))).toEqual(arrToList([2]))
|
||||
expect(middleNode(arrToList([1, 2, 3, 4, 5]))).toEqual(arrToList([3, 4, 5]))
|
||||
expect(middleNode(arrToList([1, 2, 3, 4, 5, 6]))).toEqual(arrToList([4, 5, 6]))
|
||||
})
|
@ -1,20 +1,5 @@
|
||||
import { sortList } from '../../src/list/sort-list'
|
||||
|
||||
function ListNode (val) {
|
||||
this.val = val
|
||||
this.next = null
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
import { arrToList } from './ListNode'
|
||||
|
||||
test('链表排序', () => {
|
||||
const source = [4, 2, 1, 3]
|
||||
|
Reference in New Issue
Block a user