求链表的中间节点

public class MiddleNode {

    public Node middle(Node headNode) {
        if (headNode == null) {
            System.out.println("this is no node in this list");
            return null;
        }
        Node before = headNode;
        Node behind = headNode;
        while (before != null && before.next != null) {
            before = before.next.next;
            behind = behind.next;
        }
        return behind;
    }
}
View Code

 

posted @ 2018-10-11 15:40  ZECDLLG  阅读(171)  评论(0编辑  收藏  举报