Question

In: Computer Science

Write a java method to swap between two values in a singly linked list

  • Write a java method to swap between two values in a singly linked list

Solutions

Expert Solution

// Java program to swap two given nodes of a linked list
  
class Node
{
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}
  
public class Main
{
Node head; // head of list
  
/* Function to swap Nodes x and y in linked list by
changing links */
public void swapNodes(int x, int y)
{
// Nothing to do if x and y are same
if (x == y) return;
  
// Search for x (keep track of prevX and CurrX)
Node prevX = null, currX = head;
while (currX != null && currX.data != x)
{
prevX = currX;
currX = currX.next;
}
  
// Search for y (keep track of prevY and currY)
Node prevY = null, currY = head;
while (currY != null && currY.data != y)
{
prevY = currY;
currY = currY.next;
}
  
// If either x or y is not present, nothing to do
if (currX == null || currY == null)
return;
  
// If x is not head of linked list
if (prevX != null)
prevX.next = currY;
else //make y the new head
head = currY;
  
// If y is not head of linked list
if (prevY != null)
prevY.next = currX;
else // make x the new head
head = currX;
  
// Swap next pointers
Node temp = currX.next;
currX.next = currY.next;
currY.next = temp;
}
  
/* Function to add Node at beginning of list. */
public void push(int new_data)
{
/* 1. alloc the Node and put the data */
Node new_Node = new Node(new_data);
  
/* 2. Make next of new Node as head */
new_Node.next = head;
  
/* 3. Move the head to point to new Node */
head = new_Node;
}
  
/* This function prints contents of linked list starting
from the given Node */
public void printList()
{
Node tNode = head;
while (tNode != null)
{
System.out.print(tNode.data+" ");
tNode = tNode.next;
}
}
  
/* Driver program to test above function */
public static void main(String[] args)
{
Main llist = new Main();
  
/* The constructed linked list is:
1->2->3->4->5->6->7 */
llist.push(7);
llist.push(6);
llist.push(5);
llist.push(4);
llist.push(3);
llist.push(2);
llist.push(1);
  
System.out.print("Linked list before calling swapNodes() ");
llist.printList();
  
// Swapping two values 2 and 7.
llist.swapNodes(2, 7);
  
System.out.print("\nLinked list after calling swapNodes() ");
llist.printList();
}
}

Editor Snapshot:

Output Snapshot:


Related Solutions

Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly...
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly Linked List 2. Display the list 3. Count the number of nodes 4. Insert a new node at the beginning of a Singly Linked List. 5. Insert a new node at the end of a Singly Linked List 6. Insert a new node after the value 5 of Singly Linked List 7. Delete the node with value 6. 8. Search an existing element in...
write a recursive method that returns the product of all elements in java linked list
write a recursive method that returns the product of all elements in java linked list
Write a subroutine named swap in C thatswaps two nodes in a linked list. The first...
Write a subroutine named swap in C thatswaps two nodes in a linked list. The first node should not be able to change places. The nodes are given by: Struct nodeEl { int el; struct nodeEl * next; }; typdef struct nodeEl node; The list header (of type node *) is the first parameter of the subroutine. The second and third parameters consist of integers and are the places in the list where the nodes are to change places. The...
Evaluate and write an algorithm to find the largest item in an unsorted singly linked list...
Evaluate and write an algorithm to find the largest item in an unsorted singly linked list with cells containing integers
What will be the final linked-list after executing the following method on the given input singly...
What will be the final linked-list after executing the following method on the given input singly linked-list? Consider that the singly linked-list does not have a tail reference. Input: 1->2->3->4->5->6->7->8->null                                                    void method(list){ if(list.head == null) return; Node slow_ref = list.head; Node fast_ref = list.head; Node prevS = null; Node prevF = null; while(fast_ref != null && fast_ref.next != null){ prevS = slow_ref; slow_ref = slow_ref.next; prevF = fast_ref; fast_ref = fast_ref.next.next; } prevS.next = slow_ref.next; prevF.next.next = slow_ref; slow_ref.next...
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
Using Java programming, Write a LinkedList method swap() that takes two ints as arguments and swaps...
Using Java programming, Write a LinkedList method swap() that takes two ints as arguments and swaps the elements at those two positions. The method should not traverse the list twice to find the elements, and it should not create or destroy any nodes.
Problem Description: Using Python write a Singly‐Linked List (with only the Head pointer) that supports the...
Problem Description: Using Python write a Singly‐Linked List (with only the Head pointer) that supports the following operations: 1. Adding an element at the middle of the list. 2. Removing the middle element of the list (and return the data). 3. Adding an element at a given index. 4. Removing an element at a given index (and return the data). For #1 and #2, ignore the operations if the length of the list is an even number. For #3, if...
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class in C++ that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value...
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value x if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT