File tree 3 files changed +127
-0
lines changed
solution/0200-0299/0234.Palindrome Linked List
3 files changed +127
-0
lines changed Original file line number Diff line number Diff line change @@ -202,6 +202,50 @@ public class Solution {
202
202
}
203
203
```
204
204
205
+ ### ** TypeScript**
206
+
207
+ ``` ts
208
+ /**
209
+ * Definition for singly-linked list.
210
+ * class ListNode {
211
+ * val: number
212
+ * next: ListNode | null
213
+ * constructor(val?: number, next?: ListNode | null) {
214
+ * this.val = (val===undefined ? 0 : val)
215
+ * this.next = (next===undefined ? null : next)
216
+ * }
217
+ * }
218
+ */
219
+
220
+ function isPalindrome(head : ListNode | null ): boolean {
221
+ if (head == null || head .next == null ) return true ;
222
+ // 快慢指针定位到中点
223
+ let slow: ListNode = head , fast: ListNode = head .next ;
224
+ while (fast != null && fast .next != null ) {
225
+ slow = slow .next ;
226
+ fast = fast .next .next ;
227
+ }
228
+ console .log (slow )
229
+ // 翻转链表
230
+ let cur: ListNode = slow .next ;
231
+ slow .next = null ;
232
+ let prev: ListNode = null ;
233
+ while (cur != null ) {
234
+ let t: ListNode = cur .next ;
235
+ cur .next = prev ;
236
+ prev = cur ;
237
+ cur = t ;
238
+ }
239
+ // 判断回文
240
+ while (prev != null ) {
241
+ if (prev .val != head .val ) return false ;
242
+ prev = prev .next ;
243
+ head = head .next ;
244
+ }
245
+ return true ;
246
+ };
247
+ ```
248
+
205
249
### ** ...**
206
250
207
251
```
Original file line number Diff line number Diff line change @@ -203,6 +203,50 @@ public class Solution {
203
203
}
204
204
```
205
205
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 isPalindrome(head : ListNode | null ): boolean {
222
+ if (head == null || head .next == null ) return true ;
223
+ // 快慢指针定位到中点
224
+ let slow: ListNode = head , fast: ListNode = head .next ;
225
+ while (fast != null && fast .next != null ) {
226
+ slow = slow .next ;
227
+ fast = fast .next .next ;
228
+ }
229
+ console .log (slow )
230
+ // 翻转链表
231
+ let cur: ListNode = slow .next ;
232
+ slow .next = null ;
233
+ let prev: ListNode = null ;
234
+ while (cur != null ) {
235
+ let t: ListNode = cur .next ;
236
+ cur .next = prev ;
237
+ prev = cur ;
238
+ cur = t ;
239
+ }
240
+ // 判断回文
241
+ while (prev != null ) {
242
+ if (prev .val != head .val ) return false ;
243
+ prev = prev .next ;
244
+ head = head .next ;
245
+ }
246
+ return true ;
247
+ };
248
+ ```
249
+
206
250
### ** ...**
207
251
208
252
```
Original file line number Diff line number Diff line change
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 isPalindrome ( head : ListNode | null ) : boolean {
14
+ if ( head == null || head . next == null ) return true ;
15
+ // 快慢指针定位到中点
16
+ let slow : ListNode = head , fast : ListNode = head . next ;
17
+ while ( fast != null && fast . next != null ) {
18
+ slow = slow . next ;
19
+ fast = fast . next . next ;
20
+ }
21
+ console . log ( slow )
22
+ // 翻转链表
23
+ let cur : ListNode = slow . next ;
24
+ slow . next = null ;
25
+ let prev : ListNode = null ;
26
+ while ( cur != null ) {
27
+ let t : ListNode = cur . next ;
28
+ cur . next = prev ;
29
+ prev = cur ;
30
+ cur = t ;
31
+ }
32
+ // 判断回文
33
+ while ( prev != null ) {
34
+ if ( prev . val != head . val ) return false ;
35
+ prev = prev . next ;
36
+ head = head . next ;
37
+ }
38
+ return true ;
39
+ } ;
You can’t perform that action at this time.
0 commit comments