-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path206.py
More file actions
29 lines (23 loc) · 713 Bytes
/
206.py
File metadata and controls
29 lines (23 loc) · 713 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
if (head == None):
return None
prev = None
cur = head
nextNode = head.next
while (nextNode != None):
# save this bc we overwrite it
nextNode = cur.next
# update it!
cur.next = prev
# continue iterating
prev = cur
cur = nextNode
if (cur != None):
return cur # singleton case
return prev