Skip to content

Instantly share code, notes, and snippets.

@Svastikkka
Last active August 25, 2020 19:09
Show Gist options
  • Save Svastikkka/b325d823506d6a9634f8ba45cc660231 to your computer and use it in GitHub Desktop.
Save Svastikkka/b325d823506d6a9634f8ba45cc660231 to your computer and use it in GitHub Desktop.
class Node:
def __init__(self,data):
self.data=data
self.next=None
def LinkedList(arr):
head=None
tail=None
if len(arr)<1:
return None
else:
for i in arr:
if i==-1:
break
else:
NewNode=Node(i)
if head is None:
head=NewNode
tail=NewNode
else:
tail.next=NewNode
tail=NewNode
return head
def printLL(head):
while head is not None:
print(head.data,end=" ")
head=head.next
print()
def reverseLL(head):
if head is None or head.next is None:
return head,head
smallHead,smallTail=reverseLL(head.next)
smallTail.next=head
head.next=None
return smallHead,head
# Main
from sys import setrecursionlimit
setrecursionlimit(11000)
arr=list(map(int,input().split()))
head,tail=reverseLL(LinkedList(arr))
printLL(head)
#using LL
class Node:
def __init__(self,data):
self.data=data
self.next=None
def LinkedList(arr):
head=None
if len(arr)<1:
return
for i in arr:
if i==-1:
break
else:
NewNode=Node(i)
NewNode.next=head
head=NewNode
return head
def printLL(head):
while head is not None:
print(head.data,end=" ")
head=head.next
t=int(input())
for i in range(t):
arr=list(map(int,input().split()))
printLL(LinkedList(arr))
@Svastikkka
Copy link
Author

Reverse LL (Recursive)

@Svastikkka
Copy link
Author

Reverse LL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment