Compare commits
4
Commits
64c805cf06
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f19f1f5e85 | ||
|
|
4ee6823943 | ||
|
|
1d785da008
|
||
|
|
51984f6f9b
|
@@ -0,0 +1,27 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type ListNode struct {
|
||||
Val int
|
||||
Next *ListNode
|
||||
}
|
||||
|
||||
func makeLink(arr []int) *ListNode {
|
||||
if len(arr) == 0 {
|
||||
return nil
|
||||
}
|
||||
head := &ListNode{}
|
||||
cur := head
|
||||
for _, v := range arr {
|
||||
cur.Next = &ListNode{Val: v}
|
||||
cur = cur.Next
|
||||
}
|
||||
return head.Next
|
||||
}
|
||||
func checkLink(head *ListNode) {
|
||||
for head != nil {
|
||||
fmt.Println(head.Val)
|
||||
head = head.Next
|
||||
}
|
||||
}
|
||||
@@ -1,55 +1,62 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
// ListNode Definition for singly-linked list.
|
||||
type ListNode struct {
|
||||
Val int
|
||||
Next *ListNode
|
||||
func conquerAndSort(head *ListNode, end *ListNode) *ListNode {
|
||||
if head == nil || head == end || head.Next == nil {
|
||||
return head
|
||||
}
|
||||
if head.Next == end {
|
||||
if end != nil {
|
||||
if head.Val > end.Val {
|
||||
head.Val, end.Val = end.Val, head.Val
|
||||
}
|
||||
}
|
||||
return head
|
||||
}
|
||||
if head.Next.Next == end {
|
||||
if head.Val > head.Next.Val {
|
||||
head.Next.Val, head.Val = head.Val, head.Next.Val
|
||||
}
|
||||
return head
|
||||
}
|
||||
slow, fast := head, head
|
||||
for fast.Next != nil && fast.Next.Next != nil {
|
||||
slow = slow.Next
|
||||
fast = fast.Next.Next
|
||||
}
|
||||
lastSlow := slow
|
||||
slow = slow.Next
|
||||
lastSlow.Next = nil
|
||||
a := conquerAndSort(head, nil)
|
||||
b := conquerAndSort(slow, end)
|
||||
if a.Val > b.Val {
|
||||
a, b = b, a
|
||||
}
|
||||
// a Val < b Val
|
||||
head = a
|
||||
cur := a
|
||||
a = a.Next
|
||||
for a != nil && b != nil {
|
||||
if a.Val > b.Val {
|
||||
cur.Next = b
|
||||
b = b.Next
|
||||
} else {
|
||||
cur.Next = a
|
||||
a = a.Next
|
||||
}
|
||||
cur = cur.Next
|
||||
}
|
||||
if a == nil {
|
||||
cur.Next = b
|
||||
} else {
|
||||
cur.Next = a
|
||||
}
|
||||
return head
|
||||
}
|
||||
|
||||
func isPalindrome(head *ListNode) bool {
|
||||
srcHead := head
|
||||
head = head.Next
|
||||
// var left, right *ListNode
|
||||
cnt := 1
|
||||
for head != nil {
|
||||
head = head.Next
|
||||
cnt++
|
||||
}
|
||||
head = srcHead.Next
|
||||
lastNode := srcHead
|
||||
srcHead.Next = nil
|
||||
half := cnt / 2
|
||||
half--
|
||||
for range half {
|
||||
p := head.Next
|
||||
head.Next = lastNode
|
||||
lastNode = head
|
||||
head = p
|
||||
}
|
||||
if cnt&1 == 1 {
|
||||
head = head.Next
|
||||
}
|
||||
for head != nil && lastNode != nil {
|
||||
if head.Val != lastNode.Val {
|
||||
return false
|
||||
}
|
||||
head = head.Next
|
||||
lastNode = lastNode.Next
|
||||
}
|
||||
if head != nil || lastNode != nil {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
func sortList(head *ListNode) *ListNode {
|
||||
return conquerAndSort(head, nil)
|
||||
}
|
||||
|
||||
func main() {
|
||||
isPalindrome := isPalindrome(&ListNode{
|
||||
Val: 1,
|
||||
Next: &ListNode{
|
||||
Val: 2,
|
||||
},
|
||||
})
|
||||
fmt.Printf("isPalindrome: %v\n", isPalindrome)
|
||||
arr := []int{-1, 5, 3, 4, 0}
|
||||
checkLink(sortList(makeLink(arr)))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user