I am trying to swap two nodes in doubly linked list using python. It is working in some test cases but not giving the correct output in few test cases.
Input format:
First line contains an integer denoting number of test cases.
Each test case has 4 lines. First line contains the number of elements in list, Second line contains the list elements separated by space.
Third and Fourth lines contains X and Y, the node numbers to be swapped.
Sample Input
2
6
1 2 3 4 5 6
1
5
6
1 2 3 4 5 6
1
6
Outputs: Output of first test case should be 5 2 3 4 1 6 , but I'm getting as 1 6 only in the output. And output of second test case should be 6 2 3 4 5 1 but I'm getting output as 1 only.
Code snippet below:
# Don't change this
class Node:
def __init__(self, data):
self.data = data
self.next = None
self.prev = None
def insertEnd(head, data):
if head is None:
new_node = Node(data);
head = new_node;
return head;
n = head;
while n.next is not None:
n = n.next;
new_node = Node(data);
n.next = new_node;
new_node.prev = n;
return head;
def printList(node):
while (node != None):
print(node.data, end=' ');
node = node.next;
print();
# Enter your code here
def swapNodes(head,X,Y):
if head is None or head.next is None:
return
if X == Y:
return head
def countLength(head):
current = head
count = 1
while current.next is not None:
count += 1
current = current.next
return count
size = countLength(head)
if X > size or Y > size:
return head
current = head
node1 = None
while (current != None and current.data != X):
current = current.next
node1 = current
current = head
node2 = None
while (current != None and current.data != Y):
current = current.next
node2 = current
if node1.prev == None:
head = node2
elif node2.prev == None:
head = node1
temp = node1.next
node1.next = node2.next
node2.next = temp
if node1.next != None:
node1.next.prev = node1
if node2.next != None:
node2.next.prev = node2
temp = node1.prev
node1.prev = node2.prev
node2.prev = temp
if node1.prev != None:
node1.prev.next = node1
if node2.prev != None:
node2.prev.next = node2
return head
# Don't edit this
if __name__ == "__main__":
T = int(input());
for i in range(T):
N = int(input());
head = None;
if(N!=0):
inp = input().strip().split();
for j in inp:
head = insertEnd(head,int(j.strip()));
X = int(input().strip());
Y = int(input().strip());
swapNodes(head,X,Y);
printList(head);
The template code seems to suggest that swapNodes
is not supposed to return anything, and that head
should not change. This is because in the driver code we just have this:
swapNodes(head,X,Y);
As it is clear that the first value in the list could be swapped, we must conclude that this exercise is not looking for a solution where you move whole nodes, but just move values. Otherwise it just is not possible to move the head node.
Your implementation would have worked if the driver code had looked like this:
head = swapNodes(head,X,Y);
But I assume that code is not supposed to be modified.
So... just rewrite your method, and make it swap the data
in the two identified nodes, which actually makes the job much easier -- as you will understand.