寻找两个相交的单链表的第一个节点

两个相交的单链表的第一个节点

昨天,58集团笔试,我在上面看到了一道比较常见的笔试题。就是两个相交的单链表的第一个节点。

首先,我们应该考虑清楚单链表相交的情况。单链表有些特殊。两个单链表相交一定只会有一种情况,这是我们必须知道的。这种情况就是下图。
 这就是单链表的特殊点了。那看着这个图我们应该就很清楚这个题目的做法了。我们首先算出两个链表的长度。然后我们让长链表首先遍历这个差值。接下来短链表和长链表一起遍历。直到两个链表的next是相同的时候,那么这个next就是链表的交点了。
package test20170324;

/**
 * Created by 58 on 2017/3/24.
 * author yun zhi fei
 */
public class IntersectionPointOfList {
    public static void main(String[] args) {
        Node node = new Node(1);
        Node node2 = new Node(2);
        Node node3 = new Node(3);
        Node node4 = new Node(4);
        Node node5 = new Node(5);
        Node node6 = new Node(6);
        node.setNext(node2).setNext(node4).setNext(node5).setNext(node3);
        node6.setNext(node2).setNext(node4).setNext(node5).setNext(node3);
        IntersectionPointOfList intersectionPointOfList = new IntersectionPointOfList();
        System.out.println(intersectionPointOfList.calcIntersectionPoint(node, node6));
    }

    public Node calcIntersectionPoint(Node first, Node second) {
        Node firstCopy = first;
        Node secondCopy = second;
        if (null == first || null == second) {
            return null;
        }
        Node result = null;
        int length1 = 0;
        int length2 = 0;
        while (first != null) {
            first = first.getNext();
            length1++;
        }
        while (second != null) {
            second = second.getNext();
            length2++;
        }
        boolean firstMoreLong = length1 > length2;
        int sub = 0;
        if (firstMoreLong) {
            sub = length1 - length2;
            while (sub-- > 0) {
                first = first.getNext();
            }
        } else {
            sub = length2 - length1;
            while (sub-- > 0) {
                second = second.getNext();
            }
        }
        second = secondCopy;
        first = firstCopy;
        while (first != null && second != null) {
            if (first.getNext() == second.getNext()) {
                result = first.getNext();
                break;
            } else {
                first = first.getNext();
                second = second.getNext();
            }
        }
        return result;
    }
}

class Node {
    Node next;
    int value;

    Node(int value) {
        this.value = value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public Node getNext() {
        return next;
    }

    public int getValue() {
        return value;
    }

    public Node setNext(Node next) {
        this.next = next;
        return next;
    }

    @Override
    public String toString() {
        System.out.println("the Node value = " + value);
        return "the Node value = " + value;
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值