今天和大家聊的问题叫做 给单链表加一,我们先来看题面:https://leetcode-cn.com/problems/plus-one-linked-list/
Given a non-negative integer represented as non-empty a singly linked list of digits, plus one to the integer.
You may assume the integer do not contain any leading zero, except the number 0 itself.
The digits are stored such that the most significant digit is at the head of the list.
用一个 非空 单链表来表示一个非负整数,然后将这个整数加一。你可以假设这个整数除了 0 本身,没有任何前导的 0。这个整数的各个数位按照 高位在链表头部、低位在链表尾部 的顺序排列。
示例
输入: [1,2,3] 输出: [1,2,4]
解题
这是一道linked list题,有几种情况需要考虑,第一种情况正常,末尾不为9的时候直接+1第二种情况,末尾为9的时候要向前进位置,还要判断如果全为9的话要新建一个链表头
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { public ListNode plusOne(ListNode head) { ListNode newHead = new ListNode(0); newHead.next = head; ListNode curr = newHead; ListNode curr_head = newHead; while(curr.next!=null){ curr = curr.next; if(curr.val != 9){ curr_head = curr; } } if(curr_head == curr){ curr.val++; }else{ curr_head.val++; curr = curr_head; while(curr.next != null){ curr = curr.next; curr.val = 0; } } if(newHead.val == 0){ newHead.next = null; return head; }else{ return newHead; } } }
好了,今天的文章就到这里,如果觉得有所收获,请顺手点个在看或者转发吧,你们的支持是我最大的动力 。