Created
December 3, 2020 07:37
-
-
Save ishank-dev/65a41d414365885080084265c7324f6f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node: | |
def __init__(self,data): | |
self.data = data | |
self.next = None | |
self.head = None | |
# class LinkedList: | |
# def __init__(self): | |
# self.next = None | |
# self.head = None | |
n1 = Node(1) | |
n2 = Node(2) | |
n3 = Node(3) | |
n4 = Node(4) | |
n5 = Node(5) | |
# head = LinkedList() | |
n1.next = n2 | |
n2.next = n3 | |
n3.next = n4 | |
n4.next = n5 | |
n5.next = None | |
head = n1 | |
temp = head | |
print('Before Reversing') | |
while(temp!=None): | |
print(temp.data,end = ' ') | |
temp = temp.next | |
def reverse(head): | |
prev = None | |
curr = head | |
while(curr!=None): | |
nex = curr.next | |
curr.next = prev | |
prev = curr | |
curr = nex | |
return prev | |
print('After Reversing') | |
temp = reverse(head) # just had to update the temp , logic was totally fine | |
# temp = head ---> This is what I was doing before during interview | |
while(temp!=None): | |
print(temp.data,end = ' ') | |
temp = temp.next |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment