what’s issue with this code ?
Swap Node in Pairs Python
class Node:
def __init__(self,data):
self.data=data
self.next=None
def arr_to_ll(arr):
head=None
tail=None
for i in arr:
if head is None:
head=Node(i)
tail=head
else:
tail.next=Node(i)
tail=tail.next
return head
def print_ll(head):
while head:
print(head.data,end=" -> ")
head=head.next
def swapPairs(head):
if head is None or head.next is None:
return head
ptr1=head
ptr2=head.next
ptr3=head.next.next
ptr2.next=None
swapped_head=swapPairs(ptr3)
ptr2.next=ptr1
ptr1.next=swapped_head
return ptr2
n=int(input())
arr=[int(ele) for ele in input().strip().split()]
head=arr_to_ll(arr)
ptr=head
print_ll(ptr)
print()
print_ll(swapPairs(head))
hey im not proficient with python but you can see this:
Algorithm: The above problem of swapping nodes in a pair can be solved using following algorithm whose steps are as below:
Step 1: Take linkedlist as input and pass it to a function namely ‘Swap_nodes’.
Step 2: Inside the function using recursive method swap two nodes pairwise.
i) Evertime we compare two nodes and keep on swapping them with the help of recursive function Swap_nodes() till either node is equal to null or next of node equal to null (node == null || node.next == null) which is the base case of the function.This base case will help us to avoid Null pointer Exception.
ii) The base case will return the node.
return node;
Step 3: On reaching the last two nodes of the linkedlist we will interchange the link that is pointing from secondlast element to last element, to point it in reverse direction.
Step 4: Print the linked list which is the desired output.
Pseudocode:
Swapnodes(Node node)
if (node == null || node.next == null) // Base Case
return node;
ListNode swap = Swapnodes(node.next.next);
ListNode another = node.next;
another.next = node;
node.next = swap;
return another;
@aa1 my code is also doing the same and even getting accepted on leetcode , but not on your portal.
can you check for test cases which are getting failed/wrong printed in your portal
you can see this test case:
https://s3.ap-south-1.amazonaws.com/cb-s3-bucket-ind-1/45183818-f245-493c-b2d2-ac384660a141input3.txt
the answer should be
https://s3.ap-south-1.amazonaws.com/cb-s3-bucket-ind-1/dc0dc107-bcb3-4a9f-aa81-94ca7a523a0boutput3.txt
if this doesn’t help please let me know i ll mark this for mentor attention
class Node:
def __init__(self,data):
self.data=data
self.next=None
def arr_to_ll(arr):
head=None
tail=None
for i in arr:
if head is None:
head=Node(i)
tail=head
else:
tail.next=Node(i)
tail=tail.next
return head
def print_ll(head):
while head:
print(head.data,end=" -> ")
head=head.next
def swapPairs(head):
if head is None or head.next is None:
return head
ptr1=head
ptr2=head.next
new_head=ptr2
while ptr1 and ptr2:
temp=ptr2.next
ptr2.next=ptr1
if temp and temp.next:
ptr1.next=temp.next
ptr2=temp.next
else:
ptr1.next=temp
ptr2=None
ptr1=temp
return new_head
n=int(input())
arr=[int(ele) for ele in input().strip().split()]
head=arr_to_ll(arr)
ptr=head
print_ll(ptr)
print()
print_ll(swapPairs(head))
@aa1 what about this one ? Swap Node in Pairs Python
This is also passing all test cases on leetcode, but not on you portal
Im not proficient with python. I ll mark this for mentor attention
@ritvikagrawal1
this code is passing all testcases on Hackerblock as well
check
https://hack.codingblocks.com/app/contests/1290/1429/problem
if you are unsatisfied with TA’s response you can reopen your doubt
kindly look into this doubt. it’s posted so many days ago.
WHY SO MUCH DELAY ?