29 lines
677 B
Python
29 lines
677 B
Python
def reverseList(head):
|
|
"""
|
|
:type head: ListNode
|
|
:rtype: ListNode
|
|
"""
|
|
prev,current = None,head
|
|
|
|
while current:
|
|
nxt = current.next
|
|
current.next = prev
|
|
print(prev)
|
|
prev = current
|
|
current = nxt
|
|
return prev
|
|
|
|
def reverseListRecursive(head):
|
|
# Check if head exists
|
|
if not head:
|
|
return None
|
|
|
|
newHead = head
|
|
# Check if head has more to iterate through
|
|
if head.next:
|
|
newHead = self.reverseListRecursive(head.next)
|
|
head.next.next = head
|
|
head.next = None
|
|
return newHead
|
|
|
|
reverseList([1,2,3,4,5]) |