Skip to content

Commit

Permalink
feat: solve problem 234
Browse files Browse the repository at this point in the history
  • Loading branch information
orgball2608 committed Jul 25, 2024
1 parent 3ce7fc5 commit 2221866
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions easy/234. Palindrome Linked List.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package easy

/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func isPalindromeLinkedList(head *ListNode) bool {
if head.Next == nil {
return true
}
//find middle pointer
slow, fast := head, head
for fast != nil && fast.Next != nil {
slow = slow.Next
fast = fast.Next.Next
}

//reverse list from middle element
sublist := reverseList(slow)

//compare two list
return compare(sublist, head)
}

func compare(a, b *ListNode) bool {
for a != nil && b != nil {
if a.Val != b.Val {
return false
}
a = a.Next
b = b.Next
}
return true
}

//Time: 0(N/2 + N + N)
//Space: O(1)

0 comments on commit 2221866

Please sign in to comment.