Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am getting an instance error can anyone help debug and help? class AddNums { static ListNode head1 ; static ListNode head2 ; class ListNode{

I am getting an instance error can anyone help debug and help?

class AddNums {

static ListNode head1;

static ListNode head2;

class ListNode{

int key;

ListNode next;

public ListNode(int data) {

key = data;

next = null;

}

}

public ListNode addLists(ListNode list1, ListNode list2) {

ListNode dummyHead = new ListNode(0);

ListNode p = list1, q = list2, curr = dummyHead;

int carry = 0;

while (p != null || q != null) {

int x = (p != null) ? p.key : 0;

int y = (q != null) ? q.key : 0;

int sum = carry + x + y;

carry = sum / 10;

curr.next = new ListNode(sum % 10);

curr = curr.next;

if (p != null) p = p.next;

if (q != null) q = q.next;

}

if (carry > 0) {

curr.next = new ListNode(carry);

}

return dummyHead.next;

/*

ListNode result = null;

ListNode prev = null;

ListNode temp = null;

int sum = 0;

int carry = 0;

int x,y;

while(list1 != null || list2 != null) {

if(list1 != null) {

x = list1.key;

}

else {

x = 0;

}

if(list2 != null) {

y = list2.key;

}

else {

y = 0;

}

sum = carry + x + y;

}

return result;

*/

}

void printList(ListNode head)

{

while (head != null) {

System.out.print(head.key + " ");

head = head.next;

}

System.out.println("");

}

public static void main (String args[]) {

AddNums list = new AddNums();

// creating first list

list.head1 = new ListNode(7);

list.head1.next = new ListNode(5);

list.head1.next.next = new ListNode(9);

list.head1.next.next.next = new ListNode(4);

list.head1.next.next.next.next = new ListNode(6);

System.out.print("First List is ");

list.printList(head1);

// creating second list

list.head2 = new ListNode(8);

list.head2.next = new ListNode(4);

System.out.print("Second List is ");

list.printList(head2);

// add the two lists and see the result

ListNode rs = list.addLists(head1, head2);

System.out.print("Resultant List is ");

list.printList(rs);

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

PC Magazine Guide To Client Server Databases

Authors: Joe Salemi

1st Edition

156276070X, 978-1562760700

More Books

Students also viewed these Databases questions