写点什么

第八周作业

用户头像
olderwei
关注
发布于: 2020 年 07 月 26 日

算法题

有两个单向链表(链表长度分别为 m,n),这两个单向链表有可能在某个元素合并,如下图所示的这样,也可能不合并。现在给定两个链表的头指针,在不修改链表的情况下,如何快速地判断这两个链表是否合并?如果合并,找到合并的元素,也就是图中的 x 元素。请用(伪)代码描述算法,并给出时间复杂度和空间复杂度。

实现代码如下

public class Node {
public String value;
public Node next;
public Node(String value, Node next) { this.value = value; this.next = next; }}
public class LinkListTest { public static void main(String[] args) { Node node5 = new Node("5", null); Node node4 = new Node("4", node5); Node node3 = new Node("3", node4); Node node2 = new Node("2", node3); Node node1 = new Node("1", node2); Node node0 = new Node("0", node1);
Node node10 = new Node("5", null); Node node9 = new Node("4", node10); Node node8 = new Node("3", node9); Node node7 = new Node("2", node8); Node node6 = new Node("0", node7);
String result = judgeLinkListTailEqual(node0, node6); System.out.println(result); }
public static String judgeLinkListTailEqual(Node p, Node q) { Stack<String> pStack = new Stack<>(); Stack<String> qStack = new Stack<>(); //将链表1的节点依次放入栈中 Node pHead = p; while (pHead != null) { pStack.push(pHead.value); pHead = pHead.next; } //将链表2的节点也依次放入栈中 Node qHead = q; while (qHead != null) { qStack.push(qHead.value); qHead = qHead.next; } //从两个栈中一次取出一个节点进行比较,直到不相等,这是相等的最后一个值就是合并的元素 String equalNode = null; while (!pStack.isEmpty() && !qStack.isEmpty()) { String pValue = pStack.pop(); String qValue = qStack.pop();
if (pValue.equals(qValue)) { equalNode = pValue; } else { break; } } return equalNode; }}
复制代码

输出结果为 2,该算法的时间复杂度为 O(m+n+min(m,n)),空间复杂度为 m+n。

HDFS

请画出 DataNode 服务器节点宕机的时候,HDFS 的处理过程时序图。


发布于: 2020 年 07 月 26 日阅读数: 47
用户头像

olderwei

关注

还未添加个人签名 2018.04.26 加入

还未添加个人简介

评论

发布
暂无评论
第八周作业