WB Lee WB Lee - 23 days ago 6
Java Question

ListNode head.next.next = new ListNode(0); what is this error?

I just want to build a linked list, I do not know the reason of this problem. Could someone help me? Perhaps this is easy, but I cannot figure it out.

enter image description here

how to solve this problem?

Answer

Try this

ListNode head = new ListNode(0)

head.next = new ListNode(1)

head.next.next = new ListNode(2)

Do not put ListNode in front of head.next its already defined.

Comments