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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:36:15+00:00 2026-06-02T19:36:15+00:00

Linked List Class – class MyList{ int val; MyList next; MyList(int val){ this.val =

  • 0

Linked List Class –

class MyList{
        int val;
        MyList next;
        MyList(int val){
            this.val = val;
        }

        @Override
        public String toString() {
            MyList current = this;
            String out="";
            while(current != null){
                out += current.val+"-->";
                current = current.next;
            }


            return out+"TAIL";
        }
    }

I tried and its working-

MyList list3 = list;
    MyList list4 = list.next;
    while(list3!=null && list4!=null){  
        alternateListswap(list3,list4);
        if(list3.next.next==null || list4.next.next==null)
            break;
        list3 = (list3.next.next==null)?null:list3.next.next;
        list4 = (list4.next.next==null)?null:list4.next.next;
    }


private static MyList alternateListswap(MyList L3, MyList L4) {
        int temp = L4.val;
        L4.val = L3.val;
        L3.val = temp;
        return L3;

    }

My Input –

MyList list = new MyList(1);
        list.next = new MyList(2);
        list.next.next = new MyList(3);
        list.next.next.next = new MyList(4);
        list.next.next.next.next = new MyList(5);
        list.next.next.next.next.next = new MyList(6);
        list.next.next.next.next.next.next = new MyList(7);
        list.next.next.next.next.next.next.next = new MyList(8);
        list.next.next.next.next.next.next.next.next = new MyList(9);
        list.next.next.next.next.next.next.next.next.next = new MyList(10);

My list output is – 1–>2–>3–>4–>5–>6–>7–>8–>9–>10–>TAIL

Now I want to swap the alternate nodes in the LinkedList in JAVA.

EXPETED OUTPUT – 2–>1–>4–>3–>6–>5–>8–>7–>10–>9–>TAIL

  • 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-02T19:36:17+00:00Added an answer on June 2, 2026 at 7:36 pm

    You need to swap two elements (if there are at least two in the list) then proceed to the next pair.

    Your approach currently does this: 1) go to the very end of the list (via front-recursion), then 2) swap elements as you go toward the front of the list (as you go up the recursive call chain). The result of this is to reverse the list

    You need something like this: 1) if there are at least two elements in the list (L, L.next are valid) swap the two elements, then 2) proceed two eleemnts further and repeat (e.g. alternateListswap(L.next.next);)

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

Sidebar

Related Questions

Please explain what this task is about? Create a generic linked list class that
I have a recursive object, a linked list really: public class LinkedList { public
I wrote a linked list implementation for my java class earlier this year. This
I'm making a linked list class and am trying to implement this contains() method.
This is a function within my doubly linked list class, but every time I
I want to databind a ListBox to a linked list public class MyClass {
When I attempt to insert this 'food' object into my template class linked list
This is homework I'm working on implementing a linked list class for my C++
i want to implement this in c#, i have my linked list class ready,
I have the following function defined inside my linked list class. The declaration in

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.