Question

Write the java code that will convert the given Before links to the After links with...

Write the java code that will convert the given Before links to the After links with comments.

For example:

Before:

           +----+----+    +----+----+

list ----> | 1 | +----> | 2 | / |

           +----+----+    +----+----+

After:

           +----+----+    +----+----+    +----+----+

list ----> | 1 | +----> | 2 | +----> | 3 | / |

           +----+----+    +----+----+    +----+----+

Code: (example answer)

list.next.next = new ListNode(3, null);   // 2 -> 3

(1)

Before:

           +----+----+    +----+----+

list ----> | 1 | +----> | 2 | / |

           +----+----+    +----+----+

After:

           +----+----+    +----+----+    +----+----+

list ----> | 3 | +----> | 1 | +----> | 2 | / |

           +----+----+    +----+----+    +----+----+

Code:

-----------------------------------------------------------------

(2)

Before:

           +----+----+    +----+----+

list ----> | 1 | +----> | 2 | / |

           +----+----+    +----+----+

           +----+----+    +----+----+

temp ----> | 3 | +----> | 4 | / |

           +----+----+    +----+----+

After:

           +----+----+    +----+----+    +----+----+    +----+----+

list ----> | 1 | +----> | 3 | +----> | 4 | +- --> | 2 | /

           +----+----+    +----+----+    +----+----+    +----+----+

Code:

-----------------------------------------------------------------

(3)

Before

           +----+----+    +----+----+    +----+----+

list ----> | 1 | +----> | 2 | +----> | 3 | / |

           +----+----+    +----+----+    +----+----+

After:

           +----+----+

list ----> | 2 | / |

           +----+----+

           +----+----+    +----+----+

List2 ---> | 1 | +----> | 3 | / |

           +----+----+    +----+----+

Code:

​​​​​​​-----------------------------------------------------------------

(4)

Before

           +----+----+    +----+----+    +----+----+

list ----> | 5 | +----> | 4 | +----> | 3 | / |

           +----+----+    +----+----+    +----+----+

After:

           +----+----+    +----+----+    +----+----+

list ----> | 3 | +----> | 4 | +----> | 5 | / |

           +----+----+    +----+----+    +----+----+

Code:

​​​​​​​​​​​​​​-----------------------------------------------------------------

(5)

Before:

           +----+----+

list ----> | 1 | / |

           +----+----+

           +----+----+    +----+----+    +----+----+

list2 ---> | 2 | +----> | 3 | +----> | 4 | / |

           +----+----+    +----+----+    +----+----+

After:

           +----+----+    +----+----+    +----+----+

list2 ---> | 4 | +----> | 1 | +----> | 2 | / |

           +----+----+    +----+----+    +----+----+

           +----+----+

List2 ---> | 3 | / |

           +----+----+

Code:

Homework Answers

Answer #1

1)

list = new ListNode(3, list);

2)

temp->next->next = list->next;

list->next = temp;

3)

ListNode* list2 = list;

list = list->next;

list2->next = list2->next->next;

list->next = NULL;

4)

ListNode* t = list->next->next;

t->next = list->next;

list->next->next = list;

list->next->next->next = NULL;

list = t

5)

list->next->next->next = list;

list = list->next->next;

ListNode* list2 = list->next->next;

list->next->next = NULL;

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Program in Java 1- Write a code to remove continuous repeatitive elements of a Linked List....
Program in Java 1- Write a code to remove continuous repeatitive elements of a Linked List. Example: Given: -10 -> 3 -> 3 -> 3 -> 5 -> 6 -> 6 -> 3 -> 2 -> 2 -> NULL The answer: -10 -> 3 -> 5 -> 6 -> 3 -> 2 -> NULL
program in java 1- Write a code to remove continuous repeatitive elements of a Linked List....
program in java 1- Write a code to remove continuous repeatitive elements of a Linked List. Example: Given: -10 -> 3 -> 3 -> 3 -> 5 -> 6 -> 6 -> 3 -> 2 -> 2 -> NULL The answer: -10 -> 3 -> 5 -> 6 -> 3 -> 2 -> NULL
Program in Java 1- Write a code to remove continuous repeatitive elements of a Linked List....
Program in Java 1- Write a code to remove continuous repeatitive elements of a Linked List. Example: Given: -10 -> 3 -> 3 -> 3 -> 5 -> 6 -> 6 -> 3 -> 2 -> 2 -> NULL The answer: -10 -> 3 -> 5 -> 6 -> 3 -> 2 -> NULL
I need a method for public void ****************** write the method “insertDoubles” that for each value...
I need a method for public void ****************** write the method “insertDoubles” that for each value found in an integer linked list it inserts that value doubled after the original. The method should return nothing and take in in no parameters. If the list is empty, then the method should do nothing. To avoid infinite loops you need to move the iterator past the newly inserted value. here is the provided code public class Question02 { public class ListNode//public for...
● Write code to read the content of the text file input.txt using JAVA. For each...
● Write code to read the content of the text file input.txt using JAVA. For each line in input.txt, write a new line in the new text file output.txt that computes the answer to some operation on a list of numbers. ● If the input.txt has the following: Min: 1,2,3,5,6 Max: 1,2,3,5,6 Avg: 1,2,3,5,6 Your program should generate output.txt as follows: The min of [1, 2, 3, 5, 6] is 1. The max of [1, 2, 3, 5, 6] is...
In Java 1. What will be the value of x after the following section of code...
In Java 1. What will be the value of x after the following section of code executes: int x = 3; if (x > 3)     x = x – 2; else     x = x + 2; A. 1 B.3    C.5              D.7 2. What will be the value of y after the following section of code executes: int y = 5, z = 3; if (y > 4){      z = 2;      y = y – 1;...
write a java code. Write a program using loops to compute the sum of the 30...
write a java code. Write a program using loops to compute the sum of the 30 terms of the series below. 91/(3 + 2 + 2) + 92/(4 - 4 + 5) + 93/(5 + 6 - 8) + 94/(6 - 8 + 11) + 95/(7 + 10 + 14) + 96/(8 - 12 - 17) + . . . . Output: Term Ratio Sum 1 13 13 2 18.4 31.4 etc etc
USING JAVA LANGUAGE : Using Doubly Linked List, create a java code that does the following...
USING JAVA LANGUAGE : Using Doubly Linked List, create a java code that does the following Without using LinkedList from the JAVA LIBRARY. and please include methods for each function. Create a menu that contains the following operations : 1. Add new node to DLL. ( as a METHOD ) 2. Delete a node from DLL. ( as a METHOD ) 3. Show how many nodes in DLL. ( as a METHOD ) 4. Print all data in the DLL....
1. Write a Java program to count the letters, spaces, numbers and other characters of an...
1. Write a Java program to count the letters, spaces, numbers and other characters of an input string. 2. Write a Java program to print numbers between 1 to 100 which are divisible by 3, 5 and by both 3. Write a Java program to convert a given string into lowercase
Write a Java code to complete the following operations: (1) Define a double variable var1, initialize...
Write a Java code to complete the following operations: (1) Define a double variable var1, initialize it with value 9.99 (2) Given two integer variables var2 and var3, write a statement that gives var3 a value that is 4 more than the value of var2. (3) Define a double variable var4, initialize it with value 1234.56. Write a statement to change var4 so it will just hold one third (1/3) of the original value (4) Given four double variables var5,...