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
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...
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
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,...
JAVA /** * numDistinctElements returns the number of distinct elements in a given array of doubles....
JAVA /** * numDistinctElements returns the number of distinct elements in a given array of doubles. * * Some examples: * numDistinctElements(new double[] { }) is 0 * numDistinctElements(new double[] { 1, -4, -7, 7, 8, 11 }) is 6 * numDistinctElements(new double[] { -7, -4, -7, 3, 8, 8 }) is 4 */ Code Below: public static int numDistinctElements (double[] list) { return 0; }
i want to complete this code to insert a new node in the middle of list...
i want to complete this code to insert a new node in the middle of list (take a node data from user, search the node and insert new node after this node). this is the code #include <iostream> #include <stdlib.h> using namespace std ; struct Node{                int data;                Node *link ;}; struct Node *head=NULL, *tail=NULL; /* pointers to Node*/ void InsertFront(); void InsertRear(); void DeleteFront(); void DeleteRear(); int main(){                int choice;                do{                               cout << "1:...
For Java programming code: Write methods to add and multiply 1) Two integers 2) Two decimals...
For Java programming code: Write methods to add and multiply 1) Two integers 2) Two decimals 3) One integer and one decimal
Code in JAVA The requirements are as follows: The input will be in a text file...
Code in JAVA The requirements are as follows: The input will be in a text file whose name is given by arg[0] of main(). It will contain a fully-parenthesized infix expression containing only: "(", ")", "+", "-" and integers. Need help on the main and fixing the Queue. //Input: ( ( 1 + 2 ) - ( ( 3 - 4 ) + ( 7 - 2 ) ) ) ( ( 1 + 2 ) - ( 3 -...
Write the below code to use HTML and JavaScript. 1)Write a JavaScript program to create a...
Write the below code to use HTML and JavaScript. 1)Write a JavaScript program to create a new string from a given string changing the position of first and last characters. The string length must be greater than or equal to 1. 2)Write a JavaScript program to check whether a string starts with 'Java' and false otherwise
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT