Skip to content

Commit 974dadb

Browse files
authored
Merge pull request 6boris#236 from 0xff-dev/master
Add solution and test-cases for problem 234
2 parents c80dcfe + 4af7565 commit 974dadb

File tree

6 files changed

+35
-21
lines changed

6 files changed

+35
-21
lines changed

leetcode/201-300/0234.Palindrome-Linked-List/README.md

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
11
# [234.Palindrome Linked List][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
Given the `head` of a singly linked list, return true if it is a palindrome.
75

8-
**Example 1:**
6+
**Example 1:**
7+
![exampl1](pal1linked-list.jpg)
98

109
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
10+
Input: head = [1,2,2,1]
11+
Output: true
1312
```
1413

15-
## 题意
16-
> ...
17-
18-
## 题解
14+
**Example 2:**
15+
![example2](pal2linked-list.jpg)
1916

20-
### 思路1
21-
> ...
22-
Palindrome Linked List
23-
```go
2417
```
25-
18+
Input: head = [1,2]
19+
Output: false
20+
```
2621

2722
## 结语
2823

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(head *ListNode) bool {
4+
walk := head
5+
r := true
6+
isPalindromeAux(head, &walk, &r)
7+
return r
8+
}
9+
func isPalindromeAux(head *ListNode, tail **ListNode, r *bool) {
10+
if head == nil {
11+
return
12+
}
13+
isPalindromeAux(head.Next, tail, r)
14+
res := head.Val == (*tail).Val
15+
16+
*r = *r && res
17+
*tail = (*tail).Next
518
}

leetcode/201-300/0234.Palindrome-Linked-List/Solution_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
13+
inputs *ListNode
1414
expect bool
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", &ListNode{1, &ListNode{2, &ListNode{2, &ListNode{1, nil}}}}, true},
17+
{"TestCase2", &ListNode{1, &ListNode{2, nil}}, false},
18+
{"TestCase3", &ListNode{Val: 1}, true},
1919
}
2020

2121
// 开始测试
6.97 KB
Loading
3.49 KB
Loading
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package Solution
2+
3+
type ListNode struct {
4+
Val int
5+
Next *ListNode
6+
}

0 commit comments

Comments
 (0)