Do not return anything, modify head in-place instead.But I don't see what's wrong with my code ?


  • 0
    R

    I don't know what's wrong wiht my code ?
    please help me .

    class Solution(object):

    def reverseList(self,head):
        if not head or not head.next:
            return head
        newhead=None
        while head:
            p=head
            head=head.next
            p.next=newhead
            newhead=p
        return newhead 
    def reorderList(self, head):
        if not head or not head.next or not head.next.next:
            return  head
        dummy=ListNode(0)
        dummy.next=head
        slow=fast=dummy
        while  fast.next and fast.next.next:
            slow=slow.next
            fast=fast.next.next
        headB=self.reverseList(slow.next)
        slow.next=None
        headA=dummy.next
        new=ListNode(0)
        p=new
        while headA and headB:
            p.next=headA
            p.next.next=headB
            headA=headA.next
            headB=headB.next
            p=p.next.next
        if headB:
            p.next=headB
        return new.next

  • 0
    Z

    @rachellecwx just remove return head or return newhead statement, leaving it return is fine. Because the head is assigned as a pointer to the function, no need to return it. Just don't change the value is fine.


Log in to reply
 

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