-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReverse Linked List.py
40 lines (29 loc) · 949 Bytes
/
Reverse Linked List.py
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
30
31
32
33
34
35
36
37
38
39
40
# https://leetcode.com/problems/reverse-linked-list/?envType=study-plan-v2&envId=leetcode-75
"""
Given the head of a singly linked list, reverse the list, and return the reversed list
"""
from typing import Optional
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]:
resultNode = None
currentNode = head
while currentNode:
temp = currentNode.next
currentNode.next = resultNode
resultNode = currentNode
currentNode = temp
return resultNode
node3 = ListNode(3)
node2 = ListNode(2, node3)
head = ListNode(1, node2)
solution = Solution()
reversedHead = solution.reverseList(head)
# Printing the reversed linked list
current = reversedHead
while current:
print(current.val)
current = current.next