Skip to content

Commit 9df6ef4

Browse files
committed
feat: add ts solution to lcci problem: No.02.06
No.02.06.Palindrome Linked List
1 parent fa9fd24 commit 9df6ef4

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

lcci/02.06.Palindrome Linked List/README.md

+29
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,35 @@ function isPalindrome(head: ListNode | null): boolean {
249249
}
250250
```
251251

252+
```ts
253+
/**
254+
* Definition for singly-linked list.
255+
* class ListNode {
256+
* val: number
257+
* next: ListNode | null
258+
* constructor(val?: number, next?: ListNode | null) {
259+
* this.val = (val===undefined ? 0 : val)
260+
* this.next = (next===undefined ? null : next)
261+
* }
262+
* }
263+
*/
264+
265+
function isPalindrome(head: ListNode | null): boolean {
266+
let root = head;
267+
const dfs = (node: ListNode | null): boolean => {
268+
if (node == null) {
269+
return true;
270+
}
271+
if (dfs(node.next) && node.val === root.val) {
272+
root = root.next;
273+
return true;
274+
}
275+
return false;
276+
};
277+
return dfs(head);
278+
}
279+
```
280+
252281
### **Go**
253282

254283
```go

lcci/02.06.Palindrome Linked List/README_EN.md

+29
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,35 @@ function isPalindrome(head: ListNode | null): boolean {
249249
}
250250
```
251251

252+
```ts
253+
/**
254+
* Definition for singly-linked list.
255+
* class ListNode {
256+
* val: number
257+
* next: ListNode | null
258+
* constructor(val?: number, next?: ListNode | null) {
259+
* this.val = (val===undefined ? 0 : val)
260+
* this.next = (next===undefined ? null : next)
261+
* }
262+
* }
263+
*/
264+
265+
function isPalindrome(head: ListNode | null): boolean {
266+
let root = head;
267+
const dfs = (node: ListNode | null): boolean => {
268+
if (node == null) {
269+
return true;
270+
}
271+
if (dfs(node.next) && node.val === root.val) {
272+
root = root.next;
273+
return true;
274+
}
275+
return false;
276+
};
277+
return dfs(head);
278+
}
279+
```
280+
252281
### **Go**
253282

254283
```go

0 commit comments

Comments
 (0)