返回介绍

solution / 2800-2899 / 2816.Double a Number Represented as a Linked List / README

发布于 2024-06-17 01:02:59 字数 6224 浏览 0 评论 0 收藏 0

2816. 翻倍以链表形式表示的数字

English Version

题目描述

给你一个 非空 链表的头节点 head ,表示一个不含前导零的非负数整数。

将链表 翻倍 后,返回头节点_ _head_ _。

 

示例 1:

输入:head = [1,8,9]
输出:[3,7,8]
解释:上图中给出的链表,表示数字 189 。返回的链表表示数字 189 * 2 = 378 。

示例 2:

输入:head = [9,9,9]
输出:[1,9,9,8]
解释:上图中给出的链表,表示数字 999 。返回的链表表示数字 999 * 2 = 1998 。

 

提示:

  • 链表中节点的数目在范围 [1, 104]
  • 0 <= Node.val <= 9
  • 生成的输入满足:链表表示一个不含前导零的数字,除了数字 0 本身。

解法

方法一:翻转链表 + 模拟

我们先将链表翻转,然后模拟乘法运算,最后再将链表翻转回来。

时间复杂度 $O(n)$,其中 $n$ 是链表的长度。忽略答案链表的空间消耗,空间复杂度 $O(1)$。

# Definition for singly-linked list.
# class ListNode:
#   def __init__(self, val=0, next=None):
#     self.val = val
#     self.next = next
class Solution:
  def doubleIt(self, head: Optional[ListNode]) -> Optional[ListNode]:
    def reverse(head):
      dummy = ListNode()
      cur = head
      while cur:
        next = cur.next
        cur.next = dummy.next
        dummy.next = cur
        cur = next
      return dummy.next

    head = reverse(head)
    dummy = cur = ListNode()
    mul, carry = 2, 0
    while head:
      x = head.val * mul + carry
      carry = x // 10
      cur.next = ListNode(x % 10)
      cur = cur.next
      head = head.next
    if carry:
      cur.next = ListNode(carry)
    return reverse(dummy.next)
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *   int val;
 *   ListNode next;
 *   ListNode() {}
 *   ListNode(int val) { this.val = val; }
 *   ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
  public ListNode doubleIt(ListNode head) {
    head = reverse(head);
    ListNode dummy = new ListNode();
    ListNode cur = dummy;
    int mul = 2, carry = 0;
    while (head != null) {
      int x = head.val * mul + carry;
      carry = x / 10;
      cur.next = new ListNode(x % 10);
      cur = cur.next;
      head = head.next;
    }
    if (carry > 0) {
      cur.next = new ListNode(carry);
    }
    return reverse(dummy.next);
  }

  private ListNode reverse(ListNode head) {
    ListNode dummy = new ListNode();
    ListNode cur = head;
    while (cur != null) {
      ListNode next = cur.next;
      cur.next = dummy.next;
      dummy.next = cur;
      cur = next;
    }
    return dummy.next;
  }
}
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *   int val;
 *   ListNode *next;
 *   ListNode() : val(0), next(nullptr) {}
 *   ListNode(int x) : val(x), next(nullptr) {}
 *   ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
  ListNode* doubleIt(ListNode* head) {
    head = reverse(head);
    ListNode* dummy = new ListNode();
    ListNode* cur = dummy;
    int mul = 2, carry = 0;
    while (head) {
      int x = head->val * mul + carry;
      carry = x / 10;
      cur->next = new ListNode(x % 10);
      cur = cur->next;
      head = head->next;
    }
    if (carry) {
      cur->next = new ListNode(carry);
    }
    return reverse(dummy->next);
  }

  ListNode* reverse(ListNode* head) {
    ListNode* dummy = new ListNode();
    ListNode* cur = head;
    while (cur) {
      ListNode* next = cur->next;
      cur->next = dummy->next;
      dummy->next = cur;
      cur = next;
    }
    return dummy->next;
  }
};
/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *   Val int
 *   Next *ListNode
 * }
 */
func doubleIt(head *ListNode) *ListNode {
  head = reverse(head)
  dummy := &ListNode{}
  cur := dummy
  mul, carry := 2, 0
  for head != nil {
    x := head.Val*mul + carry
    carry = x / 10
    cur.Next = &ListNode{Val: x % 10}
    cur = cur.Next
    head = head.Next
  }
  if carry > 0 {
    cur.Next = &ListNode{Val: carry}
  }
  return reverse(dummy.Next)
}

func reverse(head *ListNode) *ListNode {
  dummy := &ListNode{}
  cur := head
  for cur != nil {
    next := cur.Next
    cur.Next = dummy.Next
    dummy.Next = cur
    cur = next
  }
  return dummy.Next
}
/**
 * Definition for singly-linked list.
 * class ListNode {
 *   val: number
 *   next: ListNode | null
 *   constructor(val?: number, next?: ListNode | null) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 *   }
 * }
 */

function doubleIt(head: ListNode | null): ListNode | null {
  head = reverse(head);
  const dummy = new ListNode();
  let cur = dummy;
  let mul = 2;
  let carry = 0;
  while (head) {
    const x = head.val * mul + carry;
    carry = Math.floor(x / 10);
    cur.next = new ListNode(x % 10);
    cur = cur.next;
    head = head.next;
  }
  if (carry) {
    cur.next = new ListNode(carry);
  }
  return reverse(dummy.next);
}

function reverse(head: ListNode | null): ListNode | null {
  const dummy = new ListNode();
  let cur = head;
  while (cur) {
    const next = cur.next;
    cur.next = dummy.next;
    dummy.next = cur;
    cur = next;
  }
  return dummy.next;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文