Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a linked list in java? [duplicate]

I would like to write a linked list like this:

"a" -> "b" -> "c" -> "d"

This is what I've tried so far but it's obviously wrong. I was wondering how to express this correctly in java?

LinkedList<String> s = new LinkedList<>();
s = {"a"->"b"->"c"->"d"};

Thanks!

like image 606
munmunbb Avatar asked Aug 28 '26 02:08

munmunbb


1 Answers

That's how the pointers in the list look internally, to actually add it to the list you need to do this:

List<String> s = new LinkedList<>(); 

s.add("a"); 
s.add("b");
s.add("c");
s.add("d");
like image 179
epoch Avatar answered Aug 30 '26 17:08

epoch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!