Question: 2 (20 points) (Delete Node in a Linked List) Suppose the node of singly linked list is defined as follows. // Definition for singly-1inked 1ist
2 (20 points) (Delete Node in a Linked List) Suppose the node of singly linked list is defined as follows. // Definition for singly-1inked 1ist sode class ListNode public int val; public ListNode next; public ListNode (int z) val x; next null; public ListNode (int x, ListNode nextnode) valx; next -nextnode; (a) (10 points) Create a single linked list clas, named as MyList. Please implement the methods deleteNode and printNodeList in the class. class MyList( ListNode startnode; MyList (ListNode node) ( startnodenode; public void deleteNode (Listlode node) /I delete the node (except the tail) in che 1ist, // delete th // given only access to that node; public void printNode (ListNode node) ( System.out.printin (node.val): public void printNodeList (Listiode node) ( // print out the sub-list starting with the node Example: the linked list is 1-+23-+4- end and you are given the third node with value 3, the linked list should become 1-+2-4- end after calling your function. (b) (5 points) Write a main function to test MylList with the above example by first creating a list 123-+4 and then deleting the third node. You need to print out the results exactly like the above example. (c) (5 points) Write a method to reverse a singly linked list. Example: the linked list is 1 2-+3+4-end and you are given the third node with value 3, the linked list should become 4-+3 +2-+1- end after calling your function. lution
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
