Data Structures and Algorithms in Java
Data Structures and Algorithms in Java
6th Edition
ISBN: 9781118771334
Author: Michael T. Goodrich
Publisher: WILEY
Expert Solution & Answer
Book Icon
Chapter 3, Problem 27C

Explanation of Solution

Algorithm to swap two nodes in singly linked list:

The algorithm to swap two nodes “x” and “y” in singly linked list “L” is given below:

Algorithm:

Input: Two nodes “x” and “y”.

Output: Swap the two nodes in singly linked list “L”.

swapSingly(x, y):

  //Create a node to assign the head into it

  Node n = head;

/*Loop executes until the next node of head is not equal to "x" node. */

  while(n.getNext() != x)

/*Get next node of head and assign it into "n" node. */

  n = n.getNext();

/*Get the next node of "y" node and assign it into "v" node. */

  Node v = y.getNext();

/*Call setNext() method to set the next node as "y" node by using "n".*/

  n.setNext(y);

/*Call setNext() method to set the next node as "x" node by using "y". */

  y.setNext(x);

/*Call setNext() method to set the next node as "v" node by using "x". */

  x.setNext(v);

Explanation:

In the above algorithm,

  • This method accepts two input parameters such as “x” and “y”.
  • Create new node “n” and “v” to hold nodes “x” and “y” from singly linked lists.
  • The while loop executes until the next node of head is not equal to “x” node.
    • Get next node of head and assign it into “n” node.
  • Get the next node of “y” node and assign it into “v” node.
  • Call setNext() method to set the next node as “y” node by using “n”.
  • Call setNext() method to set the next node as “x” node by using “y”.
  • Call setNext() method to set the next node as “v” node by using “x”.
  • Finally, the two nodes “x” and “y” are swapped in singly linked list.

Algorithm to swap two nodes in doubly linked list:

The algorithm to swap two nodes “x” and “y” in singly linked list “L” is given below:

Algorithm:

Input: Two nodes “x” and “y”.

Output: Swap the two nodes in doubly linked list “L”.

swapDoubly(x, y):

/*Create a node to assign the previous node of "x" node into it...

Blurred answer
Students have asked these similar questions
Question Given a singly linked list, you need to do two tasks. Swap the first node with the last node. Then count the number of nodes and if the number of nodes is an odd number then delete the first node, otherwise delete the last node of the linked list. For example, if the given linked list is 1->2->3->4->5 then the linked list should be modified to 2->3->4->1. Because 1 and 5 will be swapped and 5 will be deleted as the number of nodes in this linked list is 5 which is an odd number, that means the first node which contains 5 has been deleted. If the input linked list is NULL, then it should remain NULL. If the input linked list has 1 node, then this node should be deleted and a new head should be returned. Sample 1: Input: NULL output: NULL. Sample 2: Input: 1 output: NULL Sample 3: Input: 1->2 output: 2 Sample 4: Input: 1->2->3 output: 2->1 Sample 5: Input: 1->2->3->4 _output: 4->2->3 Sample 6: Input: 1->2->3->4->5->6 output: 6->2->3->4->5. Input: The function takes one argument…
Implement a function to insert a node at the end of a doubly linked list. Explain the steps involved and analyze the time complexity of your implementation.
Consider a single-linked list of numbers. Write an algorithm to delete a node Before a certain node. The algorithm ask user to input a number and then searches the linked list. If such a node with input value is found algorithm deletes the previous node, otherwise, it reports that such a node is not found.
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education