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 7957623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:16:41+00:00 2026-06-04T04:16:41+00:00

Problem in Reversing a linked list without using recursion. I used this method, but

  • 0

Problem in Reversing a linked list without using recursion.

I used this method, but when i try and run this back home, I am not able to print the reverse of the linked list even though the function looks good It goes on to print the linked list in the same way as it did earlier.

Can someone help me understand what is wrong here??

class link {
    int data;
    public link nextlink;

    link(int d1) {
        data = d1;
    }
}

class List{

    link head;
    link revhead;

    List(){
        head = null;
    }

    boolean isEmpty(link head) {
       return head==null;
    }

    void insert(int d1) {
        link templink = new link(d1);
        templink.nextlink = head;
        head = templink;
    }

    void printlist(){
        link head1 = head;
        while(!isEmpty(head1)) {
            System.out.print(head1.data + " ");
            head1 = head1.nextlink;
        }
        System.out.println();
    }

    void reverse() {
        link previous=null,temp=null;
        while(isEmpty(head)) {
            temp = head.nextlink;
            head.nextlink = previous;
            previous = head;
            head = temp;
        }
    }

}

public class LinkedList {

    public static void main(String[] args) {

        List list1 = new List();

        list1.insert(10);
        list1.insert(20);
        list1.insert(30);
        list1.insert(40);
        list1.insert(50);
        list1.printlist();
        list1.reverse();
        list1.printlist();
     }
}
  • 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-06-04T04:16:42+00:00Added an answer on June 4, 2026 at 4:16 am

    There are two problems in your code. One: you check for isEmpty(head) where as you should check for !isEmpty(head). Second: when you fix first problem, then ‘head’ becomes null when loop terminates.

    The correct code with fix for above two problems:

    void reverse() {
        link previous = null, temp = null;
        while (!isEmpty(head)) {
            temp = head.nextlink;
            head.nextlink = previous;
            previous = head;
            if (temp == null) {
               break;
            }
            head = temp;
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble reversing my doublely linked deque list (with only a back sentinel)
I'm trying to implement my own list class but am having trouble reversing just
Possible Duplicate: Reversing an MD5 Hash Given this method in c# public string CalculateFileHash(string
I get this problem when referring to OpenFEC (openfec.org)-functions in my code: anders@ubuntu:~/workspace/fectest$ gcc
problem I'm using LilyPond to typeset sheet music for a church choir to perform.
Problem: I am using Windows as a guest operating system in a Virtual Machine
Problem When working with MasterPages, a common irritation I run into is that script
I am trying to solve this problem : https://www.spoj.pl/problems/CERC07S/ I have identified that i
I'm trying to calculate a lookat matrix myself, instead of using gluLookAt(). My problem
I'm referring to a problem similar to the one in this post , because

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.