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...
Given a singly linked list that contains a sequence of integers, write a method that loop...
Given a singly linked list that contains a sequence of integers, write a method that loop through each elements in this singly linked list with O(n) time complexity, and let each elements multiply 6, return the result. code needed in java! thanks in advance!
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...
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
I was supposed to conver a singly linked list to a doubly linked list and everytime...
I was supposed to conver a singly linked list to a doubly linked list and everytime I run my program the output prints a bunch of random numbers constantly until I close the console. Here is the code. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> struct node { int data; struct node *next; struct node *prev; }; //this always points to first link struct node *head = NULL; //this always points to last link struct node *tail = NULL;...
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...
c. You are given the following Java files: SLL.java that implements generic Singly Linked List, with...
c. You are given the following Java files: SLL.java that implements generic Singly Linked List, with class SLLNode listed as inner class. TestIntegerSLL.java that tests the SLL class by using a linked list of Integer. In SLL class add the following method:                                                                    public void moveToEnd (int i) It will move the element at the i -th position to the end of the list. You can assume i to be within the list, and that the first element has the...
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
HI i will write user's manual for a doubly/singly linked list , How can i write...
HI i will write user's manual for a doubly/singly linked list , How can i write User's manual ?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT