+ 1

How can I insert an integer data to my Linkedlist at specified index? Sample code below

https://code.sololearn.com/c6qsjaVZ5T0v/?ref=app

1st Jun 2021, 7:40 AM
Azalea
7 ответов
+ 2
For adding element at specific position add(int index, element) if there's not sufficient element in the list U will get an error message 'IndexOutOfBound' import java. util.LinkedList ; public class Program { public static void main(String[] args) { LinkedList<Integer> table = new LinkedList<Integer>(); table.add(5); // table. add(6); // table. add(7); // table. add(8); table. add(2,9); System.out.println(table); } } Check this code for better understanding
1st Jun 2021, 8:39 AM
Bot
Bot - avatar
+ 1
Azalea try this import java. util.LinkedList ; public class Program { public static void main(String[] args) { LinkedList<Integer> table = new LinkedList<Integer>(); table.add(5); table.add(6); System.out.println(table); } }
1st Jun 2021, 8:15 AM
Bot
Bot - avatar
+ 1
As I understand it, linked lists doesn't use indices in its operation. One just have to iterate through the nodes from one end to the other, while there is still a valid next node to walk through. You can still walk through the nodes, counting the number of nodes as index, and update the node's data as necessary (when found), or abort the operation when there is no node found at specified index.
1st Jun 2021, 8:20 AM
Ipang
0
Programmer how can I place the input 5 at index 2?
1st Jun 2021, 8:17 AM
Azalea
0
if I do, table[2] = data; it says cannot convert int to Linkedlist. how could i fix this?
1st Jun 2021, 8:32 AM
Azalea
0
Martin Taylor Programmer what if I am only allowed to use this test class? https://code.sololearn.com/ci3CpQ5UCSm9/?ref=app How could I input the data to the specified index?
1st Jun 2021, 2:07 PM
Azalea