阅读背景:

返回一个链表的中间的节点

来源:互联网 

返回一个链表的中间的节点:附上代码:

package linkedlist;

import javax.swing.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;

/**
 * Created by Administrator on 2015/11/6 0006.
 */
public class Hashuan {
    public static void main(String[] args) {
        Node h1=new Node(0);
        Node head1=h1;
        Node t1=new Node(1);
        Node t2=new Node(2);
        Node t3=new Node(3);
        Node t4=new Node(4);
        head1.next=t1;
        t1.next=t2;
        t2.next=t3;
        t3.next=t4;
        t4.next=null;

        //System.out.println(panduan(head1));
        /*int k=2;
        System.out.println(daoshuK(head1,2).value);*/

        System.out.println(findcerter(head1).value);

    }

    private static Node findcerter(Node head1) {
        if(head1==null)
            return null;
        Node p=head1;
        Node q=head1;
        while (p!=null){
            if(p.next==null)
                break;
            q=q.next;
            p=p.next.next;
        }
        return q;

    }

    static class Node{
        Node next;
        int value;

        public Node(int value) {
            this.value = value;
            next=null;
        }
    }
}
package linkedlist;

im



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: