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

这篇博客探讨了如何有效地找到链表的中间节点,提供了相应的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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;
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值