C# Solution


  • 0

    Reverse -> +1 -> Reverse back, such approach also works.

    public class Solution {
        public ListNode PlusOne(ListNode head) {
            int temp = PlusSingleNumber(head);
            
            if (temp == 1)
            {
                ListNode newHead = new ListNode(1);
                newHead.next = head;
                
                return newHead;
            }
            
            return head;
        }
        
        private int PlusSingleNumber(ListNode node)
        {
            int temp = 0;
            
            if (node.next == null)
                temp = node.val + 1;
            else
                temp = node.val + PlusSingleNumber(node.next);
                
            node.val = temp % 10;
            
            if (temp >= 10)
                return 1;
            else
                return 0;
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.