-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0206-reverse-linked-list.py
More file actions
40 lines (30 loc) · 1015 Bytes
/
Copy path0206-reverse-linked-list.py
File metadata and controls
40 lines (30 loc) · 1015 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
30
31
32
33
34
35
36
37
38
39
40
# https://leetcode.com/problems/reverse-linked-list/submissions/
# easy
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
tempHead = None
def reverseList(self, head: ListNode) -> ListNode:
if not head:
return head
# Helper function for recursion
# def helper(curr, prev):
# if curr.next != None:
# helper(curr.next, curr)
# else:
# self.tempHead = curr
# curr.next = prev
# if prev: prev.next = None
# helper(head, None)
# return self.tempHead
# Iterative
newhead = None
while(head):
temp2 = ListNode(head.val, None)
temp2.next = newhead
newhead = temp2
head = head.next
return newhead