IN JAVA. please post as copyable text code, Not an image. Linked Lists Node Mani
ID: 3836933 • Letter: I
Question
IN JAVA. please post as copyable text code, Not an image.
Linked Lists Node Manipulation: Fill in the "code" column in the 4 problems in the following table providing a solution that will turn the "before" picture into the "after" picture by modifying links between the nodes shown. You are not allowed to change any existing node's data field value and you are not allowed to construct any new nodes, but you are allowed to declare and use variables of type ListNode (often called "temp" variables). Recall the ListNode class discussed in class: public class ListNode { public int data;//data stored in this node public ListNode next;//link to next node in the//list } The value in brackets is the value of data for that node. All lists are terminated by null and the variables p and q have the value null when they do not point to anything.Explanation / Answer
1) For 1st case, let us take temporary variable 'temp' . Pseude code goes like this:- ListNode temp = p; p=p.next; q.next=temp; temp.next=null;
2)For 2nd test case,let us take temporary variable 'temp'. Pseudo code goes like this:- ListNode temp = p; p=q.next; temp.next=q; q=temp; q.next.next=null;
3)For 3rd test case,let us take temporary variable 'temp'.Pseudo code goes like this:- ListNode temp=q; q=q.next; temp.next=p.next; temp.next.next=p; tem.next.next.next=null; p=temp;
4)For 4th test case,let us take temorary variables 'temp' and ''rand'. Pseudo code goes like this:- ListNode temp=q; ListNode rand=p.next; p.next=temp; p.next .next=temp.next.next; rand.next =temp.next; rand.next.next=null; q= rand;