forked from srishilesh/Data-Structure-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path206_Reverse_linked_list
38 lines (33 loc) · 1014 Bytes
/
206_Reverse_linked_list
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
# https://leetcode.com/problems/reverse-linked-list
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
temp = head
stack = []
while(temp!=None):
stack.append(temp.val)
temp = temp.next
temp1 = head
while(temp1!=None):
temp1.val = stack.pop(len(stack)-1)
temp1 = temp1.next
return head
######################
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
if (head == None):
return None
first = head
tmp = head
while (tmp.next != None):
dum = ListNode(tmp.next.val)
dum.next = head
head = dum
tmp = tmp.next
first.next = None
return head
# https://leetcode.com/problems/reverse-linked-list/solution/