# Compile Error :: Line 3: error: class ListNode is public, should be declared in a file named ListNode.java

• Hi,

I am new to coding and was trying to implement AddTwo code using the solution that was provided already.

I am getting this error while running the code.

Can someone suggest ta way to correct this

Here is the code::

``````
public class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}

public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {

//Create a temporary node to store and return a link to the result
ListNode sumList = new ListNode(0);

//Create nodes that store values of L1 and L2 respectively
ListNode firstList = l1 , secondList = l2;

//create a current node that would be used to traverse the list
//Initialize current node to the present head of the list
ListNode currentList = sumList;

//Declare carry that will store value 0 or 1 depending upon the addition operation
int carry = 0;

//Traverse the ListNode until either of the lists reaches its end
while( firstList!=null || secondList!= null){

//Store values present in the first and second list elements
int num1 = (firstList != null) ? (firstList.val) : 0;
int num2 = (secondList !=null) ? (secondList.val) : 0;

int sum = num1 + num2 + carry;

//Update carry
carry = sum/10;

//Create next node of the Current list and give its initial value
//This is the actual result of the addition performed
currentList.next = new Listnode( sum%10 );

//Update currentList node to move to the next element
currentList = currentList.next;

//Move the first and second lists to the next element if they are not at the end
if( firstList != null ) {

firstList = firstList.next;
}
if( secondList != null ) {

secondList = secondList.next;
}

//For the next addition, provide value of carry that was thee output of the previous addition
//Carry can be either 0 or 1
if( carry == 1)
currentList.next = new ListNode (carry);
}

//Return the output
return sumList.next;
}
}
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.