-
Notifications
You must be signed in to change notification settings - Fork 236
/
Copy pathlinked-list-palindrome.py
49 lines (41 loc) · 1.16 KB
/
linked-list-palindrome.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# This is an input class. Do not edit.
class LinkedList:
def __init__(self, value):
self.value = value
self.next = None
#O(N) time and O(1) space
def linkedListPalindrome(head):
# Write your code here.
firstHalf, secondHalf = divideLinkedList(head)
secondHalf = reverseLinkedList(secondHalf)
return checkPalindrome(firstHalf, secondHalf)
def divideLinkedList(head):
firstHalf, secondHalf = head, None
slow, fast = head, head
previous = None
while fast.next is not None and fast.next.next is not None:
previous = slow
slow = slow.next
fast = fast.next.next
secondHalf = slow.next
if fast.next is None:
if previous is not None:
previous.next = None
else:
slow.next = None
return (firstHalf, secondHalf)
def reverseLinkedList(head):
previous, current = None, head
while current is not None:
temp = current.next
current.next = previous
previous = current
current = temp
return previous
def checkPalindrome(firstHalf, secondHalf):
while firstHalf is not None and secondHalf is not None:
if firstHalf.value != secondHalf.value:
return False
firstHalf = firstHalf.next
secondHalf = secondHalf.next
return True