简体   繁体   中英

Finding middle of double linked list for even number of nodes

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.

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)) { .

This is my code in java 
import java.util.LinkedList;

public class DeleteMiddleLinkedList {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<String>();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        list.add("5");

        DeleteMiddleLinkedList ds = new DeleteMiddleLinkedList();
        ds.middleDelete(list, "");

        for (String s : list) {
            System.out.println(s);
        }

    }

    public void middleDelete(LinkedList<String> list, String s) {
        int size = list.size();

        if (list == null) {
            return;
        }

        if (size % 2 == 0) {
            list.remove(size / 2);
            list.remove((size / 2) - 1);
        } else if (size % 2 != 0) {
            list.remove((size / 2));
        }

    }

}

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 bcuz i already had a static print method that prints the elements of a doubly linked list. any suggestions for a better soln?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM