I need help with linked list in java [Data structures] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

I need help with linked list in java [Data structures]

Hello everyone, I inserted a new node at the beginning of the linked list but i am not able to print it's data. It still says null Please guide me where i am doing wrong. Thank you! //initial head head = null //after inserting node - 30 at the beginnnig [30]-->null | head //after inserting node - 20 at the beginnnig [20]-->[30]-->null | head //after inserting node - 10 at the beginnnig [10]-->[20]-->[30]-->null | head Code: https://code.sololearn.com/cmYn9tuKj7Ng

1st Apr 2022, 5:50 PM
Edwin
Edwin - avatar
9 Answers
+ 3
Edwin Simply pass the newly created temp node to the head in the function before returning. https://code.sololearn.com/c1bi2PzuFQZc
1st Apr 2022, 7:49 PM
Lambda
Lambda - avatar
+ 2
your idea is this: Node temp = new Node(x); temp.next = head; head = temp; // added return head; then print System.out.println(head.data); System.out.println(head.next.data); System.out.println(head.next.next.data);
1st Apr 2022, 7:53 PM
zemiak
+ 1
you are passing object head of null value and getting same head object which is null. Node head = new Node(2); first create a head node.. you get 2 as output.. or else if you return temp, you get last object value 50.
1st Apr 2022, 6:05 PM
Jayakrishna 🇮🇳
+ 1
Jayakrishna🇮🇳 initially node head can be defined as null. It doesn't really has to be set with a value always. Check out this https://www.log2base2.com/data-structures/linked-list/inserting-a-node-at-the-beginning-of-a-linked-list.html
1st Apr 2022, 6:36 PM
Lambda
Lambda - avatar
+ 1
@Jayakrishna🇮🇳 I'm expecting the last node value to be printed i.e 50 head = insertStart(head, 23); head = insertStart(head, 30); head = insertStart(head, 50); Even if I create a node with a value at the start, it's value isn't changing. If i understand you correctly: Node head = new Node(2); head = insertStart(head, 23); head = insertStart(head, 30); head = insertStart(head, 50); System.out.println(head.data); //it prints 2 because the head reference is not changed
1st Apr 2022, 6:42 PM
Edwin
Edwin - avatar
+ 1
@zemiak Yes, you are right. I figured it out thank you.
1st Apr 2022, 7:56 PM
Edwin
Edwin - avatar
+ 1
Lambda you may not understood my point. It should get a head.data value immediately after a node inserted. But op code is null all time. initially any node is null default, not only head..!! Edwin Yes. head = temp ; will solve the problem.. But you replacing previous head data with current one I think.. So you may get trouble in access later I think.. Hope it helps.. Edit: Oh. It solved. 👍
1st Apr 2022, 8:13 PM
Jayakrishna 🇮🇳
0
@Jayakrishna🇮🇳 initially node is defined as null. While inserting the values at the beginning of the linked list head changes its next reference to the return node by temp node.
1st Apr 2022, 6:18 PM
Edwin
Edwin - avatar
0
Yes. But head also need to set a value for its data.. If head have no data, then it's no meaning to creates nodes.. what is your expected head.data value?
1st Apr 2022, 6:32 PM
Jayakrishna 🇮🇳