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

  • SEARCH
  • Home
  • 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 8595421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:25:44+00:00 2026-06-12T00:25:44+00:00

I am trying to implement LinkedList using Java, just to test my skills. I

  • 0

I am trying to implement LinkedList using Java, just to test my skills. I am stuck at one problem, where I have to append two linked lists that I have created. I get into an infinite loop here. Is there any way I can improve the code and implement the desired output ?

I/Ps :

List A : 4->3->2->1->0

List B : 4->3->2->1->0

O/P should be : 4->3->2->1->0->4->3->2->1->0

class List {
    int val;
    List next;

    public List(int val) {
        this.val = val;
    }

    public String toString() {
        String output = "";
        List current = this;

        while (current != null) {
            output += current.val + "->";
            current = current.next;
        }
        return output + "NULL";
    }
}


class AppendLinkedLists {

    static List push(List list, int num) {
        List newList = new List(num);
        newList.next = list;
        return newList;
    }

    static List appendLists(List listA, List listB) {
        if (listA == null)
            return listB;
        else {
            List tempList = listA;
            while (tempList.next.next != null) {
                tempList = tempList.next;
            }
            tempList.next.next = listB;
            return listA;           
        }
    }

    public static void main(String[] args) {
        List listA = new List(0);
        listA = push(listA, 1);
        listA = push(listA, 2);
        listA = push(listA, 3);
        listA = push(listA, 4);

        List listB = listA;

        System.out.println("Input List A : " + listA.toString());
        System.out.println("Input List B : " + listB.toString());
        listA = appendLists(listA, listB);
        System.out.println("Combined Input Lists A and B : " + listA.toString());
    }
}
  • 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-12T00:25:46+00:00Added an answer on June 12, 2026 at 12:25 am

    You don’t have 2 lists. You only have one.

       List listB = listA;
    

    assigns the reference listB to point to listA. So you’re appending listA onto itself.

    Regardless of what other issues you have, I would correct this (the simplest way being to create a listB in a similar fashion to how you’ve created listA).

    My other comment (hope you don’t mind) is that you’ve created a List object, but it has little/no behaviour of its own. Instead of creating your AppendLinkedLists class, I would put the functionality into the List object e.g. instead of:

    listA = push(listA, 1);
    

    write:

    listA.push(1);
    

    etc. So the behaviour is encapsulated within the List object. Similarly you could then write:

    listA.push(listB);
    

    by making use of overloading.

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

Sidebar

Related Questions

I am trying to implement the linked list data structure using java, it works
I am trying to implement a linked list using following code, I got segment
I have been trying to implement my own linked list class for didactic purposes.
I am trying to implement a simple linked list using c++. I am probably
I'm having trouble trying to implement a linked List without using classes(we're not there
I am trying to implement a simple HashTable in Java that uses a Linked
Hi I am trying to implement a linked list using templates and ADT. At
I am trying to implement a linked-list in C++. Currently, I have the following
I was trying to implement a Piece Table data structure using Linked Lists in
Here is code in which I am trying to implement a queue using linked

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.