Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Java Code Error - Reversing a Linked List

  1. #1
    Junior Member
    Join Date
    Oct 2022
    Location
    Pune
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Java Code Error - Reversing a Linked List

    Hello,

    I'm having an issue with a piece of code I'm writing in Java to reverse a linked list: https://www.interviewbit.com/blog/re...a-linked-list/. I've been trying to debug it for a few hours but I'm still having issues. The code is as follows:
    public void reverseLinkedList(Node head) { 
        Node current, next, prev; 
        current = head; 
        prev = null; 
     
        while (current != null) { 
            next = current.next; 
            current.next = prev; 
            prev = current; 
            current = next; 
        } 
        head = prev; 
    }
    The error I'm getting is "Error: null pointer exception." I'm not sure why this is happening and would appreciate any help I can get.

    Thanks in advance!
    Last edited by Norm; April 28th, 2023 at 01:09 PM. Reason: Added code tags

Similar Threads

  1. Replies: 1
    Last Post: January 12th, 2021, 06:38 AM
  2. [Linked List] Problems deleting items from a linked list
    By KLVTZ in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 8th, 2013, 09:21 PM
  3. [Linked List] Problems deleting items from a linked list
    By KLVTZ in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 8th, 2013, 07:52 PM
  4. [SOLVED] Matlab / Java Linked List Error
    By cgraider in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 13th, 2012, 09:51 AM
  5. Singly Circular Linked List Error
    By clydefrog in forum Collections and Generics
    Replies: 7
    Last Post: March 5th, 2012, 08:17 PM