- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathReverseLL.py
More file actions
Latest commit
23 lines (20 loc) · 691 Bytes
/
Copy pathReverseLL.py
File metadata and controls
23 lines (20 loc) · 691 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
classSolution(object):
defreverseList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
reversed=self.reverseNodes(head, None)
returnreversed
defreverseNodes(self, forward_head, backward_head):
ifnotforward_head:
returnbackward_head
forward_head_next=forward_head.next
backward_head_next=forward_head
backward_head_next.next=backward_head
returnself.reverseNodes(forward_head_next, backward_head_next)