列表

详情


369. 给单链表加一

给定一个用链表表示的非负整数, 然后将这个整数 再加上 1

这些数字的存储是这样的:最高位有效的数字位于链表的首位 head 。

 

示例 1:

输入: head = [1,2,3]
输出: [1,2,4]

示例 2:

输入: head = [0]
输出: [1]

 

提示:

相似题目

加一

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
/** * 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) { } };

golang 解法, 执行用时: 0 ms, 内存消耗: 1.9 MB, 提交时间: 2023-10-17 15:49:40

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func plusOne(head *ListNode) *ListNode {
    // 哨兵头节点
    sentinel := &ListNode{}
    sentinel.Next = head
    not_nine := sentinel

    // 找出最右边的非九位数
    for head != nil {
        if head.Val != 9 {
            not_nine = head
        }
        head = head.Next
    }

    // 最右边的非九位数加 1
    not_nine.Val += 1
    not_nine = not_nine.Next

    // 将所有 9 设置为 0
    for not_nine != nil {
        not_nine.Val = 0
        not_nine = not_nine.Next
    }

    if sentinel.Val != 0 {
        return sentinel
    }
    return sentinel.Next
}

cpp 解法, 执行用时: 4 ms, 内存消耗: 8.8 MB, 提交时间: 2023-10-17 15:44:25

/**
 * 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* sentinel = new ListNode(0);
       sentinel->next = head;
       ListNode* notNine = sentinel;

       // 找出最右边的非九位数
       while (head != nullptr) {
           if (head->val != 9) notNine = head;
           head = head->next;
       }
       // 最右边的非九位数加 1
       notNine->val++;
       notNine = notNine->next;
       // 将所有 9 设置为 0
       while (notNine != nullptr) {
           notNine->val = 0;
           notNine = notNine->next;
       }

       delete notNine;
       return sentinel->val != 0 ? sentinel : sentinel->next;
   }
};

python3 解法, 执行用时: 48 ms, 内存消耗: 15.7 MB, 提交时间: 2023-10-17 15:44:03

# 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:
        # 哨兵头节点
        sentinel = ListNode(0)
        sentinel.next = head
        not_nine = sentinel

        # 找出最右边的非九位数
        while head:
            if head.val != 9:
                not_nine = head
            head = head.next

        # 最右边的非九位数加 1
        not_nine.val += 1
        not_nine = not_nine.next

        # 将所有 9 设置为 0
        while not_nine:
            not_nine.val = 0
            not_nine = not_nine.next

        return sentinel if sentinel.val else sentinel.next

java 解法, 执行用时: 0 ms, 内存消耗: 39.2 MB, 提交时间: 2023-10-17 15:42:37

/**
 * 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) {
        //1.双指针
        ListNode fast = head, slow = new ListNode(0);
        slow.next = head;

        //2.遍历链表
        while (fast != null) {
            if (fast.val != 9) slow = fast;
            fast = fast.next;
        }

        //3.末位加1
        slow.val += 1;
        ListNode cur = slow.next;
        while (cur != null) {
            cur.val = 0;
            cur = cur.next;
        }
        return slow.next == head ? slow : head;
    }
}

cpp 解法, 执行用时: 0 ms, 内存消耗: 8.7 MB, 提交时间: 2023-10-17 15:38:21

/**
 * 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*ans=new ListNode(1,head);
        if(function(ans->next)==1){
            return ans;
        }else return ans->next;
    }
    
    int function(ListNode* p){
         if(p==nullptr){
             return 1;
         }
         p->val+=function(p->next);
         if(p->val==10){
             p->val=0;
             return 1;
         }else{
             return 0;
         } 
     }
};

上一题