forked from wangzheng0822/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* type ListNode struct { | ||
* Val int | ||
* Next *ListNode | ||
* } | ||
*/ | ||
func isPalindrome(head *ListNode) bool { | ||
var slow *ListNode = head | ||
var fast *ListNode = head | ||
var prev *ListNode = nil | ||
var temp *ListNode = nil | ||
|
||
if (head == nil || head.Next == nil) { | ||
return true | ||
} | ||
|
||
for (fast != nil && fast.Next !=nil){ | ||
fast = fast.Next.Next | ||
temp = slow.Next | ||
slow.Next = prev | ||
prev = slow | ||
slow = temp | ||
} // 快的先跑完,同时反转了一半链表,剪短 | ||
|
||
if fast != nil { | ||
slow = slow.Next // 处理余数,跨过中位数 | ||
// prev 增加中 2->1->nil | ||
} | ||
|
||
var l1 *ListNode = prev | ||
var l2 *ListNode = slow | ||
|
||
for (l1 != nil && l2 !=nil && l1.Val == l2.Val){ | ||
l1 = l1.Next | ||
l2 = l2.Next | ||
} | ||
|
||
return (l1 == nil && l2 == nil) | ||
|
||
} |