File tree 2 files changed +58
-0
lines changed
lcci/02.06.Palindrome Linked List
2 files changed +58
-0
lines changed Original file line number Diff line number Diff line change @@ -249,6 +249,35 @@ function isPalindrome(head: ListNode | null): boolean {
249
249
}
250
250
```
251
251
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
+
252
281
### ** Go**
253
282
254
283
``` go
Original file line number Diff line number Diff line change @@ -249,6 +249,35 @@ function isPalindrome(head: ListNode | null): boolean {
249
249
}
250
250
```
251
251
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
+
252
281
### ** Go**
253
282
254
283
``` go
You can’t perform that action at this time.
0 commit comments