Reversing a linked list in python

前端 未结 11 2354
失恋的感觉
失恋的感觉 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条回答
  •  春和景丽
    2020-12-08 13:12

    def reverseLinkedList(head):
    
        current =  head
        previous = None
        nextNode = None
    
        while current:
    
            nextNode = current.nextNode
            current.nextNode = previous
    
            previous = current
            current = nextNode
    
        return previous
    

提交回复
热议问题