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

avatar
@智云健康

文摘目录

原创

翻译

文章与新闻

推荐库

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

每周一练

苹果面试题

提供以下两个链表:

4 -> 5 -> 7 -> 83 -> 5 -> 8,每一个链表代表一个相反顺序的数字,因此:

4 -> 5 -> 7 -> 8代表:8754

3 -> 5 -> 8代表:853

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

// 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;
}