[LeetCode] 369. Plus One Linked List 链表加一运算

2021年09月15日 阅读数:1
这篇文章主要向大家介绍[LeetCode] 369. Plus One Linked List 链表加一运算,主要内容包括基础应用、实用技巧、原理机制等方面,希望对大家有所帮助。

Given a non-negative number represented as a singly linked list of digits, plus one to the number.html

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

Example:node

Input:
1->2->3

Output:
1->2->4

给一个节点为非负数的链表,链表头是高位,进行加1运算。python

解法: 因为链表没法经过坐标来直接访问元素,从链尾开始操做就很麻烦。若是链尾是高位,进行加1运算就方便了,因此先把链表翻转一下,进行加1运算后,再把链表翻转回来便可。git

Java:post

public ListNode plusOne(ListNode head) {
    ListNode h2 = reverse(head);
 
    ListNode p=h2;
 
    while(p!=null){
        if(p.val+1<=9){
            p.val=p.val+1;
            break;
        }else{
            p.val=0;
            if(p.next==null){
                p.next = new ListNode(1);
                break;
            }
            p=p.next;
        }
    }
 
    return reverse(h2);
}
 
public ListNode reverse(ListNode head){
    if(head==null||head.next==null)
        return head;
 
    ListNode p1=head;
    ListNode p2=p1.next;
    while(p2!=null){
        ListNode t = p2.next;
        p2.next=p1;
        p1=p2;
        p2=t;
    }
 
    head.next=null;
 
    return p1;
}  

Python:url

# Time:  O(n)
# Space: O(1)

# Definition for singly-linked list.
class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None


# Two pointers solution.
class Solution(object):
    def plusOne(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if not head:
            return None

        dummy = ListNode(0)
        dummy.next = head

        left, right = dummy, head
        while right.next:
            if right.val != 9:
                left = right
            right = right.next

        if right.val != 9:
            right.val += 1
        else:
            left.val += 1
            right = left.next
            while right:
                right.val = 0
                right = right.next

        return dummy if dummy.val else dummy.next

Python:htm

# Time:  O(n)
# Space: O(1)
class Solution2(object):
    def plusOne(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        def reverseList(head):
            dummy = ListNode(0)
            curr = head
            while curr:
                dummy.next, curr.next, curr = curr, dummy.next, curr.next
            return dummy.next

        rev_head = reverseList(head)
        curr, carry = rev_head, 1
        while curr and carry:
            curr.val += carry
            carry = curr.val / 10
            curr.val %= 10
            if carry and curr.next is None:
                curr.next = ListNode(0)
            curr = curr.next

        return reverseList(rev_head)  

C++:blog

class Solution {
public:
    ListNode* plusOne(ListNode* head) {
        if (!head) return head;
        ListNode *rev_head = reverse(head), *cur = rev_head, *pre = cur;
        int carry = 1;
        while (cur) {
            pre = cur;
            int t = cur->val + carry;
            cur->val = t % 10;
            carry = t / 10;
            if (carry == 0) break;
            cur = cur->next;
        }
        if (carry) pre->next = new ListNode(1);
        return reverse(rev_head);
    }
    ListNode* reverse(ListNode *head) {
        if (!head) return head;
        ListNode *dummy = new ListNode(-1), *cur = head;
        dummy->next = head;
        while (cur->next) {
            ListNode *t = cur->next;
            cur->next = t->next;
            t->next = dummy->next;
            dummy->next = t;
        }
        return dummy->next;
    }
};

C++: Recursiveget

class Solution {
public:
    ListNode* plusOne(ListNode* head) {
        if (!head) return head;
        int carry = helper(head);
        if (carry == 1) {
            ListNode *res = new ListNode(1);
            res->next = head;
            return res;
        }
        return head;
    }
    int helper(ListNode *node) {
        if (!node) return 1;
        int carry = helper(node->next);
        int sum = node->val + carry;
        node->val = sum % 10;
        return sum / 10;
    }
};

    

 

相似题目:

[LeetCode] 66. Plus One 加一

 

All LeetCode Questions List 题目汇总