Reversing a linked list in python

前端 未结 11 2367
失恋的感觉
失恋的感觉 2020-12-08 12:28

I am asked to reverse a which takes head as parameter where as head is a linked list e.g.: 1 -> 2 -> 3 which was returned from a function already defined I tried to implemen

11条回答
  •  Happy的楠姐
    2020-12-08 13:12

    U can use mod function to get the remainder for each iteration and obviously it will help reversing the list . I think you are a student from Mission R and D

    head=None   
    prev=None
    for i in range(len):
        node=Node(number%10)
        if not head:
            head=node
        else:
            prev.next=node
        prev=node
        number=number/10
    return head
    

提交回复
热议问题