• 369. Plus One Linked List


    /**
    * Lock by Leetcode
    * 369. Plus One Linked List
    * https://www.lintcode.com/problem/plus-one-linked-list/description
    *
    * Given a non-negative integer represented as non-empty a singly linked list of digits, plus one to the integer.
    You may assume the integer do not contain any leading zero, except the number 0 itself.
    The digits are stored such that the most significant digit is at the head of the list.
    Example
    Given head = 1 -> 2 -> 3 -> null, return 1 -> 2 -> 4 -> null.
    */
    /**
    * @param head: the first Node
    * @return: the answer after plus one
    */
    const plusOne = function (head) {
      let str = "";
      while (head != null) {
        str += head.val;
        head = head.next;
      }
      let resultStr = addTwoString(str, "1");
      let arr = resultStr.split("");
      let newHead = new ListNode(parseInt(arr[0]));
      createList(newHead, 1, arr);
      return newHead;
    };
    var addTwoString = function (str1, str2) {
      let len1 = str1.length - 1, len2 = str2.length - 1;
      let carry = 0;
      let res = [];
      while (len1 >= 0 || len2 >= 0) {
      let a = len1 >= 0 ? str1.charAt(len1--) - '0' : 0;
      let b = len2 >= 0 ? str2.charAt(len2--) - '0' : 0;
      let temp = a + b + carry;
      if (temp >= 10) {
        carry = Math.floor(temp / 10);//10位
        temp = Math.floor(temp % 10);//个位
      } else
        carry = 0;
      res.unshift(temp);
      }
      if (carry)
      res.unshift(1);

      return res.join("");

    };
    var createList = function (head, index, arr) {
      if (index >= arr.length)
        return;
      let node = new ListNode(parseInt(arr[index]));
      head.next = node;
      createList(node, index + 1, arr.concat());
    };
  • 相关阅读:
    Array对象
    属性描述对象
    Object对象
    console对象与控制台
    编程风格
    错误处理机制
    13、百钱买百鸡之数学优化
    12、c程序中编程,统计一行中数字字符的个数。
    10、输入某年某月某日,判断这一天是这一年的第几天?
    9、c语言输入一个整数怎么分别输出它的每位上的数字
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/10204418.html
Copyright © 2020-2023  润新知