Question: Please modify the given code below so that the node and linked list classes can store objects of any type (not only integers). package code;

Please modify the given code below so that the node and linked list classes can store objects of any type (not only integers).

package code;

public class Code { static class Node { int data; Node next; };

private static Node root;

private static Node insert(Node root, int val) { Node temp = new Node(); temp.data = val; temp.next = root; root = temp; return root; }

private static Node arrToLL(int arr[], int n) { root = null; for (int i = n - 1; i >= 0; i--) root = insert(root, arr[i]); return root; }

public static Node reverse(Node node) { Node prev = null; Node current = node; Node next = null; while (current != null) { next = current.next; current.next = prev; prev = current; current = next; } node = prev; return node; }

static void display(Node root) { while (root != null) { System.out.print(root.data + " "); root = root.next; } System.out.print(" "); }

public static void main(String[] args) { int[] intArray = new int[] { 10, 15, 35, 67, 89, 101 };

Node root = arrToLL(intArray, 6); System.out.print("Elements of the list: "); display(root); Node new_root = reverse(root); System.out.print("Elements of list after reversing: "); display(new_root); }

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!