Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6806453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:42:44+00:00 2026-05-26T19:42:44+00:00

I am writing a Java function to reverse a linked list in-place. I am

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:42:45+00:00Added an answer on May 26, 2026 at 7:42 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a java servlet that calls a function in a jar. I have
I'm writing a Java program, and I want a function that, given a string,
I am writing a user defined function to DB2 on AS/400 in Java and
Possible Duplicate: java - Array brackets after variable name When writing a Java function
I am writing a function in Java for an Android application that uses a
Im very new to java (mainly duel with embedded only) Im writing a simple
I'm writing an import function of XML files to my Java application. I am
I'm in Eclipse writing Java. I come from Visual Studio with Resharper writing C#.
I've been writing Java for the last couple of years , and now I've
Sometimes while writing Java in Eclipse, I write code that generates warnings. A common

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.