加一的链表

加一链表

题目:

描述
给定一个非负整数,这个整数表示为一个非空的单链表,
每个节点表示这个整数的一位。返回这个整数加一。
除了0本身,所有数字在最高位前都没有0。
列表的头节点存的是这个整数的最高位。

样例
样例1
输入: 1 -> 2 -> 3 -> null
输出: 1 -> 2 -> 4 -> null
解释:
123 + 1 = 124

样例2
输入: 9 -> 9 -> null
输出: 1 -> 0 -> 0 -> null
解释:
99 + 1 = 100

解题思路:本题有两种思路,一种是用栈存储每个节点后进行加一操作,另一种是利用递归

栈:

/**
 * Definition for ListNode
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */

public class Solution {
    /**
     * @param head: the first Node
     * @return: the answer after plus one
     */
    public ListNode plusOne(ListNode head) {
        if(head == null) {
            return null;
        }

        Stack<ListNode> stack = new Stack();
        ListNode cur = head;
        while(cur != null) {
            stack.push(cur);
            cur = cur.next;
        }

        ListNode tail = stack.pop();
        tail.val += 1;
        boolean flag = false;
        if(tail.val >= 10) {
            tail.val -= 10;
            flag = true;
        }

        while(flag && !stack.isEmpty()) {
            ListNode node = stack.pop();
            node.val += 1;
            if(node.val >= 10) {
                node.val -= 10;
                flag = true;
            } else {
                flag = false;
            }
        }

        if(stack.isEmpty() && flag) {
            ListNode first = new ListNode(1);
            first.next = head;
            head = first;
        }

        return head;
    }
}

递归:

/**
 * Definition for ListNode
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */

public class Solution {
    /**
     * @param head: the first Node
     * @return: the answer after plus one
     */

    private int flag = 0;
    public ListNode plusOne(ListNode head) {
        // Write your code here
        if(head == null) {
            return null;
        }

        head = dfs(head);
        if(flag == 1) {
            ListNode node = new ListNode(1);
            node.next = head;
            head = node;
        }
        return head;
    }

    public ListNode dfs(ListNode head) {
        if(head == null) {
            return null;
        }

        if(head.next == null) {
            head.val += 1;
            if(head.val >= 10) {
                head.val -= 10;
                flag = 1;
            }
            return head;
        }

        head.next = dfs(head.next);
        if(flag == 1) {
            head.val += 1;
            flag = 0;
        }

        if(head.val >= 10) {
            head.val -= 10;
            flag = 1;
        }

        return head;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值