返回介绍

solution / 0300-0399 / 0369.Plus One Linked List / README_EN

发布于 2024-06-17 01:04:01 字数 3782 浏览 0 评论 0 收藏 0

369. Plus One Linked List

中文文档

Description

Given a non-negative integer represented as a linked list of digits, _plus one to the integer_.

The digits are stored such that the most significant digit is at the head of the list.

 

Example 1:

Input: head = [1,2,3]
Output: [1,2,4]

Example 2:

Input: head = [0]
Output: [1]

 

Constraints:

  • The number of nodes in the linked list is in the range [1, 100].
  • 0 <= Node.val <= 9
  • The number represented by the linked list does not contain leading zeros except for the zero itself. 

Solutions

Solution 1

# Definition for singly-linked list.
# class ListNode:
#   def __init__(self, val=0, next=None):
#     self.val = val
#     self.next = next
class Solution:
  def plusOne(self, head: ListNode) -> ListNode:
    dummy = ListNode(0, head)
    target = dummy
    while head:
      if head.val != 9:
        target = head
      head = head.next
    target.val += 1
    target = target.next
    while target:
      target.val = 0
      target = target.next
    return dummy if dummy.val else 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 plusOne(ListNode head) {
    ListNode dummy = new ListNode(0, head);
    ListNode target = dummy;
    while (head != null) {
      if (head.val != 9) {
        target = head;
      }
      head = head.next;
    }
    ++target.val;
    target = target.next;
    while (target != null) {
      target.val = 0;
      target = target.next;
    }
    return dummy.val == 1 ? dummy : 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* plusOne(ListNode* head) {
    ListNode* dummy = new ListNode(0, head);
    ListNode* target = dummy;
    while (head) {
      if (head->val != 9) target = head;
      head = head->next;
    }
    ++target->val;
    target = target->next;
    while (target) {
      target->val = 0;
      target = target->next;
    }
    return dummy->val == 1 ? dummy : dummy->next;
  }
};
/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *   Val int
 *   Next *ListNode
 * }
 */
func plusOne(head *ListNode) *ListNode {
  dummy := &ListNode{0, head}
  target := dummy
  for head != nil {
    if head.Val != 9 {
      target = head
    }
    head = head.Next
  }
  target.Val++
  target = target.Next
  for target != nil {
    target.Val = 0
    target = target.Next
  }
  if dummy.Val == 1 {
    return dummy
  }
  return dummy.Next
}

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

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

发布评论

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