/
check_if_linklist_is_palindrome.py
67 lines (50 loc) · 1.33 KB
/
check_if_linklist_is_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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# -*- coding: UTF-8 -*-
# Program to verify if a link list is a palindrome
import initialize
def compare(head, head2):
if not head and not head2:
return True
if not head or not head2:
return False
while head and head2:
if head.data != head2.data:
return False
head = head.nextnode
head2 = head2.nextnode
return True
def reverse(head):
curr = head
prev = None
while curr:
next = curr.nextnode
curr.nextnode = prev
prev = curr
curr = next
return prev
def check_palindrome(head):
if not head or not head.nextnode:
return True
fast_ptr = head
slow_ptr = head
while fast_ptr and fast_ptr.nextnode:
prev_slow_ptr = slow_ptr
slow_ptr = slow_ptr.nextnode
fast_ptr = fast_ptr.nextnode.nextnode
if fast_ptr:
mid_point = slow_ptr
slow_ptr = slow_ptr.nextnode
second_half = slow_ptr
prev_slow_ptr.next = None
# Reverse second half of linked list
head_second_half = reverse(second_half)
# Compare two lists if they are equal or not
result = compare(head, head_second_half)
head_second_half = reverse(head_second_half)
if mid_point:
slow_ptr.nextnode = mid_point
mid_point.nextnode = head_second_half
else:
prev_slow_ptr.nextnode = second_half
return result
head = initialize.initialize_linked_list()
print check_palindrome(head)