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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:33:18+00:00 2026-06-11T10:33:18+00:00

Following is my code to find middle node of double linked list but it

  • 0

Following is my code to find middle node of double linked list but it works only for odd number of nodes. i cannot figure out how to take care of case of even number of nodes:

public Object findMiddleNode() {
    DLNode first = getHeadNode();
    DLNode last = getTailNode();
    while(first!=last) {
        first = first.getNext();
        last = last.getPrevious();
    }
    return first.getElement();      
}

this is my updated code:

public MyDequeue findMiddleNode() {
    DLNode first = getHeadNode();
    DLNode last = getTailNode();
    MyDequeue m = new MyDequeue();
    while(first!=last && first.getNext()!= last) {
        first = first.getNext();
        last = last.getPrevious();
    }
    if(first == last) {
        m.insertLast(first.getElement());           
        return m;
    }           
    else {
        m.insertLast(first.getElement());
        m.insertLast(last.getElement());
        return m;
    }
}

the class MyDequeue is my implementation of a doubly linked list. the reason I returned MyDequeue is because I already had a static print method that prints the elements of a doubly linked list.

  • 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-11T10:33:19+00:00Added an answer on June 11, 2026 at 10:33 am

    For an even number of nodes, first and last pass each other at the middle. For example, if you have 4 nodes 1..4, first will be 2 when last is 3. At the next iteration, first will be 3, and last will be 2. So you never fail the while loop test (first != last) needed to exit the loop at the proper point. A simple fix is to modify your while test to be

    while ((first != last) && (first.getNext() != last)) {.

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

Sidebar

Related Questions

To find the next odd number for an input the following code is being
When i write the following code Post.find(:all,:group=>'comments') It only gives me the first record
I using the following code to find people on twitter. but it gives me
I have written the following code to find out the missing sequence but i
I wrote the following function which returns the middle element of a linked list,
I have written the following code to find the current location. It works well
I am writing the following code to find out the required strings are present
OK, I've been using the following code to find any URLS in the text
The following code is supposed to find electors that do not have a postal
The following code: var constant = Expression.Constant(find me, typeof(string)); // memberExpression evaluates to a

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.