Skip to content

Commit 886984f

Browse files
authored
feat: add js and ts solutions to locf problem: No.06. Reverse Print (doocs#424)
1 parent d81bc26 commit 886984f

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

lcof/面试题06. 从尾到头打印链表/README.md

+49
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,30 @@ class Solution {
101101
}
102102
```
103103

104+
### **JavaScript**
105+
106+
```js
107+
/**
108+
* Definition for singly-linked list.
109+
* function ListNode(val) {
110+
* this.val = val;
111+
* this.next = null;
112+
* }
113+
*/
114+
/**
115+
* @param {ListNode} head
116+
* @return {number[]}
117+
*/
118+
var reversePrint = function(head) {
119+
let res = [];
120+
while (head != null) {
121+
res.unshift(head.val);
122+
head = head.next;
123+
}
124+
return res;
125+
};
126+
```
127+
104128
### **Go**
105129

106130
```go
@@ -179,6 +203,31 @@ public:
179203
};
180204
```
181205

206+
### **TypeScript**
207+
208+
```ts
209+
/**
210+
* Definition for singly-linked list.
211+
* class ListNode {
212+
* val: number
213+
* next: ListNode | null
214+
* constructor(val?: number, next?: ListNode | null) {
215+
* this.val = (val===undefined ? 0 : val)
216+
* this.next = (next===undefined ? null : next)
217+
* }
218+
* }
219+
*/
220+
221+
function reversePrint(head: ListNode | null): number[] {
222+
let res: number[] = [];
223+
while (head != null) {
224+
res.unshift(head.val);
225+
head = head.next;
226+
}
227+
return res;
228+
};
229+
```
230+
182231
### **...**
183232

184233
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* val: number
5+
* next: ListNode | null
6+
* constructor(val?: number, next?: ListNode | null) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.next = (next===undefined ? null : next)
9+
* }
10+
* }
11+
*/
12+
13+
function reversePrint(head: ListNode | null): number[] {
14+
let res: number[] = [];
15+
while (head != null) {
16+
res.unshift(head.val);
17+
head = head.next;
18+
}
19+
return res;
20+
};

0 commit comments

Comments
 (0)