2

I know the problem with my code is (should be) stupid. But would appreciate all help.

enter image description here

public void transferFrom(LinkedIntList list2) {

   // Point to first node of list1
   ListNode current = front;

   // Move to the last node of list1
   while(current != null) {
      current = current.next;
   }

   // Last node of list1 -> firs node of list2
   current.next = list2;

   list2 = null;

}

Problem line is current.next = list2;. Data type mismatch because current.next is ListNode and list2 is LinkedIntList.

If I rather use current.next = list2;, I get NullPointerException for this line.

What should I be doing?

EDIT: Fixed!

public void transferFrom(LinkedIntList list2) {

   // Point to first node of list1
   ListNode current = front;

   // Move to the last node of list1
   while(current != null && current.next != null) {
      current = current.next;
   }

   // Last node of list1 -> first node of list2
    if(front == null) {
        front = list2.front;
    } else {
        current.next = list2.front;
   }

   list2.front = null;

}
4

2 に答える 2

1

you want to move down the list while current.next isn't equal to null. Otherwise you "jump" off of the list onto a null pointer.

For example...

while(current.next != null){

 current = current.next

}

Once the next node is null the loop will exit and current will equal the last node on the list.

So, that'll get you as far as your last element of list one. Then just append all of the elements of list 2 to the end of list one (hint: current should point to the first element of list 2)

EDIT

As Ted Hopp stated below the info you're giving on your particular linkedlist implementations is kinda spotty so I'll explain a general case..

You should assign current to be equal to the node you current have in storage. This should probably begin as the first node in the list. In your example above you seem to be doing that but I have no idea where your front comes from. Where is it assigned its value? Where is it declared? I'd guess your front is null probably because you're pointing it at the front node incorrectly.

于 2013-01-23T01:55:22.230 に答える
1

It would help if you posted the class definitions for LinkedIntList and ListNode, and also told us what this method is actually supposed to be doing. But I'm assuming that a LilnkedIntList contains a ListNode front member and that you are trying to append the contents of list2 to this (which is another LinkedIntList). Your troublesome line should probably be:

current.next = list2.front;

However, you have another problem: your while loop is guaranteed to exit with current == null, which is not what you want at all. The loop condition should be (current.next != null), not (current != null).

Finally, if you want to empty out list2, the way to do it is list2.front = null;. Assigning to list2 inside the method does nothing.

Here's a version of your code incorporating all of my suggestions:

public void transferFrom(LinkedIntList list2) {

   if (front == null) {
       front = list2.front;
   } else {
       // Point to first node of list1
       ListNode current = front;

       // Move to the last node of list1
       while(current.next != null) {
          current = current.next;
       }

       // Last node of list1 -> firs node of list2
       current.next = list2;
   }

   // empty out list2
   list2.front = null;

}
于 2013-01-23T01:55:32.767 に答える