Answered step by step
Verified Expert Solution
Question
1 Approved Answer
the output should look like below! Create a static method in the IntNode class called repeat that repeats each node in a LinkedList. A LinkedList
the output should look like below!
Create a static method in the IntNode class called repeat that repeats each node in a LinkedList. A LinkedList (1, 2, 3), becomes (1, 1, 2, 2, 3, 3) after calling the repeat method with the head node. Here is an example run:
IntNode{num=1} IntNode{num=2} IntNode{num=3} IntNode{num=4} IntNode{num=5} IntNode{num=1} IntNode{num=1} IntNode{num=2} IntNode{num=2} IntNode{num=3} IntNode{num=3} IntNode{num=4} IntNode{num=4} IntNode{num=5} IntNode{num=5} |
this is the code i have so far... public class IntNode { private int num; private IntNode next; public IntNode(int num, IntNode next) { this.num = num; this.next = next; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } public IntNode getNext() { return next; } public void setNext(IntNode next) { this.next = next; } @Override public String toString() { return "IntNode{" + "num=" + num + '}'; } public static void main(String[] args) { IntNode five = new IntNode(5, null); IntNode four = new IntNode(4, five); IntNode three = new IntNode(3, four); IntNode two = new IntNode(2, three); IntNode head = new IntNode(1, two); //test your repeat method here printLinkedList(head); repeat(head); } public static void printLinkedList(IntNode node) { while (node != null) { System.out.print(node + " "); node = node.next; } System.out.println(); } public static void repeat(IntNode node) { while (node != null) { node.next = new IntNode(node.getNum(), node.next.next.next); System.out.print(node.next + " "); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started