File tree Expand file tree Collapse file tree 4 files changed +58
-0
lines changed
print-immutable-linked-list-in-reverse Expand file tree Collapse file tree 4 files changed +58
-0
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,8 @@ leetcode 测试
10
10
11
11
##### 包含的内容如下
12
12
13
+ https://leetcode.cn/problems/print-immutable-linked-list-in-reverse/
14
+
13
15
https://leetcode.cn/problems/plus-one-linked-list/
14
16
15
17
https://leetcode.cn/problems/minimum-swaps-to-make-sequences-increasing/
Original file line number Diff line number Diff line change
1
+ export class ImmutableListNode {
2
+ #print;
3
+ #index = 0 ;
4
+ #array: number [ ] ;
5
+ constructor ( array : Array < number > , print : ( value : number ) => void ) {
6
+ if ( array . length === 0 ) throw Error ( "input empty array" ) ;
7
+ this . #print = print ;
8
+ this . #array = array ;
9
+ }
10
+ printValue ( ) {
11
+ this . #print( this . #array[ this . #index] ) ;
12
+ }
13
+ getNext ( ) : ImmutableListNode | null {
14
+ if ( this . #index+ 1 >= this . #array. length ) return null ;
15
+ const next = new ImmutableListNode ( this . #array, this . #print) ;
16
+ next . #index = this . #index + 1 ;
17
+ return next ;
18
+ }
19
+ }
Original file line number Diff line number Diff line change
1
+ import { ImmutableListNode } from "./ImmutableListNode.ts" ;
2
+
3
+ export default function printLinkedListInReverse (
4
+ head : ImmutableListNode | null
5
+ ) {
6
+ if ( head !== null ) {
7
+ printLinkedListInReverse ( head . getNext ( ) ) ;
8
+ head . printValue ( ) ;
9
+ }
10
+ }
Original file line number Diff line number Diff line change
1
+ import { ImmutableListNode } from "./ImmutableListNode.ts" ;
2
+ import printLinkedListInReverse from "./index.ts" ;
3
+ import { assertEquals } from "https://deno.land/[email protected] /testing/asserts.ts" ;
4
+
5
+ Deno . test ( "print-immutable-linked-list-in-reverse" , ( ) => {
6
+ const cases = [
7
+ [
8
+ [ 1 , 2 , 3 , 4 ] ,
9
+ [ 4 , 3 , 2 , 1 ] ,
10
+ ] ,
11
+ [
12
+ [ 0 , - 4 , - 1 , 3 , - 5 ] ,
13
+ [ - 5 , 3 , - 1 , - 4 , 0 ] ,
14
+ ] ,
15
+ [
16
+ [ - 2 , 0 , 6 , 4 , 4 , - 6 ] ,
17
+ [ - 6 , 4 , 4 , 6 , 0 , - 2 ] ,
18
+ ] ,
19
+ ] ;
20
+ cases . forEach ( ( [ input , output ] ) => {
21
+ const result : number [ ] = [ ] ;
22
+ const node = new ImmutableListNode ( input , ( v ) => result . push ( v ) ) ;
23
+ printLinkedListInReverse ( node ) ;
24
+
25
+ assertEquals ( output , result ) ;
26
+ } ) ;
27
+ } ) ;
You can’t perform that action at this time.
0 commit comments