0

I am writing a Java function to reverse a linked list in-place. I am new to Java and fail to be successfully debug the bug in the function below. The IDE returns a NullPointerException that I can't seem to figure out. Any help would be much appreciated.

public listNode reverseLinkedList(listNode head)
{
    listNode reversedLinkedList = head;
    listNode temp = null;

    while (reversedLinkedList != null) {
        reversedLinkedList.setNext(temp);
        temp = reversedLinkedList;
        reversedLinkedList = reversedLinkedList.getNext();    
    }
    return reversedLinkedList;
}

For testing, I have a linked list intialized already with 8 linked nodes. In the function above, I am trying to swap the next pointers between each nodes and move reversedLinkedList pointer along the linked list.

EDIT: Some have suggested that I use debugger to identify where the NullPointerException occurs. I already did that. It occurs in the line reversedLinkedList = reversedLinkedList.getNext(); in the second iteration of the while loop.

I initialized the linked list with 8 pointers for testing. The while loop, instead of going to the second node, instead sees a null pointer. It may have something to do with the temp variable but I am not sure.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
user976078
  • 237
  • 3
  • 7
  • If you run it in the debugger, it should halt on the line with the exception, giving you plenty of clues. – Oliver Charlesworth Nov 15 '11 at 08:59
  • As Oli pointed out, you should get a specific line telling you where the exception occurs. By glancing over it, make sure that (if the list is singly-linked) you aren't de-referencing a node before you switch its pointers -- that would just clear the list (except for head). – filpa Nov 15 '11 at 09:02
  • So should the exception stack trace. ;) – Peter Lawrey Nov 15 '11 at 09:02
  • Some have suggested that i use debugger to identify where the nullexception occurs. I already did that. It occurs in the line "reversedLinkedList = reversedLinkedList.getNext();" in the second iteration of the while loop I initialized the linked list with 8 pointers for testing. The while loop, instead of going to the second node, instead sees a null pointer. It may have something to do with the temp variable but i am not sure. – user976078 Nov 15 '11 at 09:05

3 Answers3

3

There is flaw in your reasoning. When you (first step)

reversedLinkedList.setNext(temp)

You are changing the first reference to null (which is correct) but you loose any reference to the next pointer. Two lines later you

reversedLinkedList = reversedLinkedList.getNext();

temp was null so reversedLinkedList will be null.

You need to store the next pointer to the temporary variable before changing it.

But this is not causing an exception. Please post the complete code and the stack trace of the exception

Matteo
  • 14,696
  • 9
  • 68
  • 106
  • i get it. Thanks. Trivial bug at the end. cant believe i missed it – user976078 Nov 15 '11 at 09:15
  • I think this is causing the exception, because in the next iteration `reversedLinkedList.setNext(temp);` is executed. And since `reversedLinkedList` was set to `null` as you mentioned ... – Ben van Gompel Nov 15 '11 at 09:15
  • @BenvanGompel should not since the loop is guarded by `reversedLinkedList != null`. This is why I said that this code is most likely not causing the exception. – Matteo Nov 15 '11 at 09:20
  • @Matteo, yeah you're correct. Missed that. – Ben van Gompel Nov 15 '11 at 09:21
1

I would suggest you;

  • Read the error message to find the exact line the NullPointerException occurs.
  • Use the debugger to step through the code and see why the variable is null.

There isn't any code here which can produce a null pointer exception so I assume you are looking in the wrong place. I suggest you follow the two steps above instead of guessing.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
-1

The problem may be with the final node, you will not be able to get the next node since its NULL!

 reversedLinkedList = reversedLinkedList.getNext();

Not sure though!

dimazaid
  • 1,673
  • 3
  • 22
  • 24