In addLastMethod loop is working infinite times please check once | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

In addLastMethod loop is working infinite times please check once

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

1st Sep 2020, 6:32 AM
Shivam Rawal
2 Answers
+ 3
You should be using a constructor within the LinkedList class to handle the internal class Node instantiation and this class should be private. LinkedList.Node node1= L.new Node(1); LinkedList.Node node2 =L. new Node(2); LinkedList.Node node3 =L.new Node(4); This way you won't create a circular reference to the same object at both ends of the LinkedList like you have when adding node3 twice. Once with addFirst then again with addLast. If you comment out either of those lines your print will work as expected. L.addFirst(node1); L.addFirst(node2); L.addFirst(node3); // reference to node3 at head L.addLast(node3); // reference to node3 at tail node3 / \ node1 <---- node2 https://code.sololearn.com/chWzlf68bxiz/?ref=app https://code.sololearn.com/cCYegZHyKeAl/?ref=app
1st Sep 2020, 7:09 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Tysm
1st Sep 2020, 11:15 AM
Shivam Rawal