O(n) time, O(1) time complexity in python, need your reviews please


  • 0
    A
    # Definition for singly-linked list.
    # class ListNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution(object):
        def reverseList(self,node):
            newNode=None
            while node:
                node.next,node,newNode=newNode,node.next,node
            return newNode
        def isPalindrome(self, node):
            """
            :type head: ListNode
            :rtype: bool
            """
            head=node
            Len=0; step=0;i=0;revNode=None;
         
            while node is not None:
           
                Len+=1
                node=node.next
            
            if Len%2==0:
                step=Len//2
            else:
                step=Len//2+1
            node=head    
            for i in range(step):
                print(node.val)
                node=node.next
            revNode=self.reverseList(node)    
            node=head
       
            i=0
            while node is not None and  revNode is not None and i<=step-1:
                i+=1
                if node.val == revNode.val:
                    node=node.next
                    revNode=revNode.next
                else:
                    return False        
            return True
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.