Reverse Linked List

Reverse a singly linked list.


A linked list can be reversed either iteratively or recursively. Could you implement both?

Subscribe to see which companies asked this question.

Solving method:

class ListNode(object):
     def __init__(self, x):
         self.val = x = None
class Solution(object):
    def reverseList(self, head):
        result = ListNode(None)
        while head:
            next =
   = head
            head = next