reverse linked list with python

41

def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:

    previousNode, currentNode = None, head

    while currentNode:

        temp = currentNode.next
        currentNode.next = previousNode
        previousNode = currentNode
        currentNode = temp

    return previousNode

Comments

Submit
0 Comments