-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
41 lines (34 loc) · 904 Bytes
/
test.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
from typing import List
class ListNode(object):
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def isPalindrome( head):
if not head:
return True
dummy = None
slow, fast = head, head
prev = dummy
while fast and fast.next:
fast = fast.next.next
nxt = slow.next
slow.next = prev
prev = slow
slow = nxt
# if we want to restore the original linkedlist, in case the func belongs to a big program
# (did not implement in this script!)
first = prev
second = slow if not fast else slow.next
while first and second:
if first.val != second.val:
return False
first = first.next
second = second.next
ob1 = ListNode(1)
ob2 = ListNode(2)
ob3 = ListNode(2)
ob4 = ListNode(1)
ob1.next = ob2
ob2.next = ob3
ob3.next = ob4
isPalindrome(ob1)