-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
60aa928
commit 76ecf13
Showing
3 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |