Skip to content

Commit

Permalink
feat: add listNode
Browse files Browse the repository at this point in the history
  • Loading branch information
supperchong committed Mar 8, 2021
1 parent 60aa928 commit 76ecf13
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ export { MaxPQ, MinPQ } from './pq'
export { SkipList } from './skiplist'
export { MaxArr, SumArr, ValueArr } from './segment'
export { UnionFind } from './unionFind'
export * as listNode from './listNode'
64 changes: 64 additions & 0 deletions src/listNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
export class ListNode<T> {
constructor(
public val: T | undefined = undefined,
public next: ListNode<T> | null = null
) { }
}
/**
* Encodes a list to a single string.
*
* @mermaid
* graph LR
* 1 --- 2
* 2 --- 3
* 3 --- 4
* 4 --- 5
* @example
* ```js
* serialize(root) // => '[1,2,3,4,5]'
* ```
*/
export function serialize<T>(head: ListNode<T> | null): string {
if (!head) {
return JSON.stringify([]);
}
let arr = [];
let node: ListNode<T> | null = head;
while (node) {
arr.push(node.val);
node = node.next;
}
return JSON.stringify(arr);
}

/**
* Decodes your encoded data to list.
*/
export function deserialize<T = number>(originData: string): ListNode<T> | null {
const data = JSON.parse(originData)
let header = new ListNode<T>();
let node = header;
for (let i = 0; i < data.length; i++) {
node.next = new ListNode(data[i]);
node = node.next;
}
return header.next;
}
/**
* reverse a list
*/
export function reverseList<T>(head: ListNode<T> | null): ListNode<T> | null {
if (!head) {
return null;
}
let prev = null;
let cur: ListNode<T> | null = head;
while (cur) {
let next: ListNode<T> | null = cur.next;
cur.next = prev;
prev = cur;
cur = next;
}
return prev;
}

43 changes: 43 additions & 0 deletions test/listNode.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import assert from 'assert'
import { serialize, deserialize, ListNode } from '../src/listNode'
describe('test serialize', () => {
it('should return [] if the list is null', () => {
assert.strictEqual(serialize(null), '[]')
})
it('should return the serialize string', () => {
const node1 = new ListNode(1)
const node2 = new ListNode(2)
const node3 = new ListNode(3)

const node4 = new ListNode(4)
const node5 = new ListNode(5)
node1.next = node2
node2.next = node3
node3.next = node4
node4.next = node5
assert.strictEqual(serialize(node1), '[1,2,3,4,5]')
})
})

describe('test deserialize', () => {
it('should return null', () => {
assert.strictEqual(deserialize('[]'), null)
})
it('should return listNode', () => {
const node1 = deserialize('[1,2,3,4,5]')
assert.ok(node1)
const node2 = node1.next
assert.ok(node2)
const node3 = node2.next
assert.ok(node3)
const node4 = node3.next
assert.ok(node4)
const node5 = node4.next
assert.ok(node5)
assert.strictEqual(node1.val, 1)
assert.strictEqual(node2.val, 2)
assert.strictEqual(node3.val, 3)
assert.strictEqual(node4.val, 4)
assert.strictEqual(node5.val, 5)
})
})

0 comments on commit 76ecf13

Please sign in to comment.