2020年8月第4周 - 智云健康前端技术每周文摘(含苹果面试题)

文摘目录

原创

翻译

文章与新闻

推荐库

  • jstime: Rust编写的js运行时
  • umami:开源的网站分析工具
  • embla-carousel:一个超流畅的React滚动组件

每周一练

苹果面试题

提供如下两个链表:javascript

4 -> 5 -> 7 -> 83 -> 5 -> 8,每个链表表明一个相反顺序的数字,所以:前端

4 -> 5 -> 7 -> 8表明:8754java

3 -> 5 -> 8表明:853node

请编写一个方法来将两个数字相加并将其做为另外一个链表返回?git

// list1: 4 -> 5 -> 7 -> 8
// list2: 3 -> 5 -> 8

addLLNums(list1, list2);

// 应该返回:7 -> 1 -> 6 -> 9
// 8764 + 853 = 9617
复制代码
// Available Data Structures
function Node(val) {
  this.val = val;
  this.next = null;
}

function LinkedListNode(val) {
  this.val = val;
  this.next = null;
}

var list1 = new LinkedListNode(3);
var nodes1 = [4, 5, 6, 7, 8, 9, 10];
createNodes(list1, nodes1);

var list2 = new LinkedListNode(1);
var nodes2 = [2, 3, 4, 5, 6, 7, 8];
createNodes(list2, nodes2);

function createNodes(head, nodes) {
  for (let i = 0; i < nodes.length; i++) {
    var newNode = new LinkedListNode(nodes[i]);
    head.next = new newNode;
    head = newNode;
  }
}

/* * @param {LinkedListNode} list1 * @param {LinkedListNode} list2 * @return {LinkedListNode} */

function addLLNums(list1, list2) {
  // add list1 and list2
  return result;
}
复制代码
相关文章
相关标签/搜索