第八 周 性能优化(二)作业
发布于: 2020 年 11 月 15 日
单向链表
package com.company.gitdemo;
//单向链表public class LinkedList {    /**链表的头节点*/    Node head = null;
    /**     * 链表添加节点:     * 找到链表的末尾节点,把新添加的数据作为末尾节点的后续节点     * @param data     */    public void addNode(int data){        Node newNode = new Node(data);
        if(head == null){            head = newNode;            return;        }
        Node temp = head;
        while(temp.next != null){            temp = temp.next;        }
        temp.next = newNode;    }
    /**     * 求链表的长度     * @return     */    public int length(){        int length = 0;        Node curNode = head;
        while(curNode != null){            length++;            curNode = curNode.next;        }
        return length;    }}
复制代码
 
链表节点
package com.company.gitdemo;
//链表节点public class Node {    //下一个节点    Node next = null;    //节点数据    int data;
    public Node(int data){        this.data = data;    }}
复制代码
 寻找合并点
package com.company.gitdemo;
public class LinkedListMerged {    //找合并点    public static int findMergePoint(LinkedList a,LinkedList b) throws Exception {        boolean hasPoint=false;        int point=0;        int size1=a.length();        int size2=b.length();
        if(size1==0 || size2==0){            throw new Exception("找不到合并点");        }
        Node node1=a.head;        Node node2=b.head;
         if(size1>size2) {             int len = size1 - size2;
             for (int i = 0; i < len; i++) {                 node1 = node1.next;             }
         }else if(size1<size2) {             int len = size2 - size1;
             for (int i = 0; i < len; i++) {                 node2 = node2.next;             }         }
        while (node1 !=null && node2!=null) {            if (node1.data == node2.data) {                point = node1.data;                hasPoint = true;                break;            }
            node1 = node1.next;            node2 = node2.next;        }
        if(!hasPoint) {            throw new Exception("找不到合并点");        }
         return point;    }}复制代码
 测试用例
package com.company.gitdemo;
public class Main {
    public static void main(String[] args) throws Exception {       /* LinkedList a=new LinkedList();        a.addNode(2);        a.addNode(5);        a.addNode(1);        a.addNode(7);
        LinkedList b=new LinkedList();        b.addNode(3);        b.addNode(1);        b.addNode(7);*/
        LinkedList a=new LinkedList();        a.addNode(2);        a.addNode(5);        a.addNode(1);        a.addNode(7);        LinkedList b=new LinkedList();        b.addNode(1);        b.addNode(7);
        try {            int result=LinkedListMerged.findMergePoint(a, b);            System.out.println("交叉点值"+result);;        }        catch (Exception ex){            System.out.println(ex.getMessage());        }    }}
复制代码
 此题有两种解法,一种是暴力查找,遍历 m 链表去 n 链表去查找相同数据的节点,即为合并点,时间复杂度为 o(m*n),一种是遍历较长的链表,假设是 m,查找到 m 节点 m-start 剩余长度与 n 的全部长度相等,接下来同时遍历 m 与 n(m 从 m-start 开始,n 从头节点开始),找到相同数据的节点即为合并点
划线
评论
复制
发布于: 2020 年 11 月 15 日阅读数: 24

钟杰
关注
还未添加个人签名 2019.02.12 加入
还未添加个人简介











 
    
评论