前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >单链表判断是否有环和环起始节点

单链表判断是否有环和环起始节点

作者头像
GavinZhou
发布2018-01-02 16:05:43
4770
发布2018-01-02 16:05:43
举报

面试的滴滴研究院暑期实习生,岗位机器学习,二面除了电话面还要视频面试写代码,两个问题:

  • 单链表判断是否有环以及找出环开始的节点
  • 建立二叉排序树并进行中序遍历

因为第二个之前有写过,所以没什么问题的过了;第一个其实也不难,但是有点紧张,最后面试官告诉我判断是否有环的函数写错了,哎。。。特此重新写下,mark之

代码语言:javascript
复制
package acmcoder;

class Node_t{
    int value;
    Node_t next;
    public Node_t(int value, Node_t next) {
        this.value = value;
        this.next = next;
    }
    public Node_t() {
        value = -999;
        next = null;
    }

    @Override
    public String toString() {
        return "Node_t: value=" + value;
    }


}

public class Test_didi {
    private boolean hasLoop(Node_t head){
        //使用快慢指针
        Node_t p = head.next;
        Node_t q = head.next;
        boolean res = false;
        while(q != null && q.next != null){
            p = p.next; 
            q = q.next.next;
            if(p == q){
                res = true;
                break;
            }
        }
        return res;
    }

    private Node_t crashNode(Node_t head){
        //假定一定有环
        //使用快慢指针
        Node_t p = head.next;
        Node_t q = head.next;
        while(q.next != null){
            p = p.next;
            q = q.next.next;
            if(p == q){
                break;
            }
        }
        return p;
    }

    private Node_t findCircleStartNode(Node_t head){
        boolean hasCircle = hasLoop(head);
        if(!hasCircle){
            return new Node_t();
        }else{
            //碰撞点到连接点的距离等于头指针到连接点的距离
            Node_t crash = crashNode(head);
            Node_t start = head.next;
            while(start != crash){
                start = start.next;
                crash = crash.next;
            }
            return crash;
        }
    }

    private int circleLen(Node_t head){
        //找到碰撞点之后从碰撞点出发然后再次碰撞
        Node_t crash = crashNode(head);
        Node_t p = crash;
        Node_t q = crash;
        int len = 0;
        while(q.next != null){
            p = p.next;
            q = q.next.next;
            len++;
            if(p == q){
                break;
            }
        }
        return len;
    }

    public static void main(String[] args) {
        //建立单链表
        Node_t node1 = new Node_t(1, null);
        Node_t node2 = new Node_t(2, null);
        Node_t node3 = new Node_t(3, null);
        Node_t node4 = new Node_t(4, null);
        Node_t node5 = new Node_t(5, null);
        Node_t node6 = new Node_t(6, null);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        node5.next = node6;
        node6.next = node4;
        //判断是否有环
        Test_didi test = new Test_didi();
        boolean hs = test.hasLoop(node1);
        System.out.println("Has circle: " + hs);
        //找出环的起点
        Node_t circleStart = test.findCircleStartNode(node1);
        System.out.println("Circle starts: " + circleStart.toString());
        //计算环的长度
        int len = test.circleLen(node1);
        System.out.println("Circle length: " + len);

    }

}

虽然面的不好, 好在还是拿到了offer, 侥幸

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-03-28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档