forked from iamshubhamg/Leet-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Singly_LL.py
73 lines (56 loc) · 1.16 KB
/
Singly_LL.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
68
69
70
71
72
73
# Python3 program to check if linked
# list is palindrome using stack
class Node:
def __init__(self,data):
self.data = data
self.ptr = None
# Function to check if the linked list
# is palindrome or not
def ispalindrome(head):
# Temp pointer
slow = head
# Declare a stack
stack = []
ispalin = True
# Push all elements of the list
# to the stack
while slow != None:
stack.append(slow.data)
# Move ahead
slow = slow.ptr
# Iterate in the list again and
# check by popping from the stack
while head != None:
# Get the top most element
i = stack.pop()
# Check if data is not
# same as popped element
if head.data == i:
ispalin = True
else:
ispalin = False
break
# Move ahead
head = head.ptr
return ispalin
# Driver Code
# Addition of linked list
one = Node(1)
two = Node(2)
three = Node(3)
four = Node(4)
five = Node(3)
six = Node(2)
seven = Node(1)
# Initialize the next pointer
# of every current pointer
one.ptr = two
two.ptr = three
three.ptr = four
four.ptr = five
five.ptr = six
six.ptr = seven
seven.ptr = None
# Call function to check palindrome or not
result = ispalindrome(one)
print("isPalindrome:", result)