import java.util.*; class Node { int value; Node left, right; public Node(int value) { this.value = value; left = right = null; } public void setValue(int value) { this.value = value; } } public class Main { public static void main(String[] args) { Node root = new Node(1); root.left = new Node(2); root.right = new Node(3); root.left.left = new Node(4); root.left.right = new Node(5); System.out.println("Before:"); printTree(root); // Set the value of the root node to 10 root.setValue(10); System.out.println("After:"); printTree(root); } public static void printTree(Node node) { if (node == null) return; System.out.print(node.value + " "); printTree(node.left); printTree(node.right); } }
import java.util.*; class Node { int value; Node next; public Node(int value) { this.value = value; next = null; } public void setValue(int value) { this.value = value; } } public class Main { public static void main(String[] args) { Node head = new Node(1); head.next = new Node(2); head.next.next = new Node(3); head.next.next.next = new Node(4); head.next.next.next.next = new Node(5); System.out.println("Before:"); printList(head); // Set the value of the second node to 10 head.next.setValue(10); System.out.println("After:"); printList(head); } public static void printList(Node node) { while (node != null) { System.out.print(node.value + " "); node = node.next; } System.out.println(); } }In this example, we create a linked list and set the value of the second node to 10 using Node.setValue. Package Library: This code does not use any package library.