给定一个用单链表表示的整数,然后把这个整数加一。
数据范围:链表长度满足 ,链表上每个节点的值满足 ,可以保证链表在非 0 的情况下没有前导零
例如输入{1,2,3}时,对应的输出为{1,2,4},转换过程如下图所示:
import java.util.*; /* * public class ListNode { * int val; * ListNode next = null; * public ListNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @return ListNode类 */ public ListNode plusOne (ListNode head) { // write code here int size=0; ListNode tmp=head; //计算链表长度 while(tmp!=null){ size++; tmp=tmp.next; } //创建数组 int arr[]=new int[size]; int i=0; tmp=head; //将链表值放进数组 while(tmp!=null){ arr[i]=tmp.val; tmp=tmp.next; i++; } //如果为true需进位 boolean add=true; //进行+1 for(i=arr.length-1;i>=0;i--){ //最后一位小于9直接+1返回 if(arr[i]<9){ arr[i]++; add=false; break; }else{ //否则置为0,上一位进位 arr[i]=0; } } //如果为进位标记,进行进位 if(add==true){ arr=new int[size+1]; arr[0]=1; } //将数组值放置到链表 ListNode tmpNode=new ListNode(arr[0]); ListNode returnList=tmpNode; for(i=1;i<arr.length;i++){ ListNode node=new ListNode(arr[i]); tmpNode.next=node; tmpNode=tmpNode.next; } return returnList; } }
public ListNode plusOne (ListNode head) { if(head == null){ return head; } // write code here ListNode dummy = new ListNode(-1); dummy.next = reverse(head); ListNode cur = dummy.next; int carry = (cur.val + 1) / 10; cur.val = (cur.val + 1) % 10; cur = cur.next; while(cur != null && carry > 0){ int sum = cur.val + carry; cur.val = sum % 10; carry = sum / 10; cur = cur.next; } if(carry > 0){ ListNode newHead = new ListNode(carry); newHead.next = dummy.next; return newHead; } return reverse(dummy.next); } private ListNode reverse(ListNode head){ ListNode pre = null; ListNode next = null; while(head != null){ next = head.next; head.next = pre; pre = head; head = next; } return pre; }
import java.util.*; /* * public class ListNode { * int val; * ListNode next = null; * public ListNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @return ListNode类 */ public ListNode plusOne (ListNode head) { ListNode notNineNode = null; ListNode pre = new ListNode(0); pre.next = head; ListNode node = pre; while (node != null) { if (node.val != 9) { notNineNode = node; } node = node.next; } if (notNineNode != null) { notNineNode.val++; notNineNode = notNineNode.next; } while (notNineNode != null) { notNineNode.val = 0; notNineNode = notNineNode.next; } return pre.val != 0 ? pre : pre.next; } }
static boolean f=true,r=true,s=true; static ListNode pre=null; public ListNode plusOne (ListNode head) { if(r){ pre=head; pre.val+=1; r=false; } if(head==null){ return head; } ListNode node=plusOne(head.next); ListNode p=node; if(p==null&&head.val>9){ ListNode pp=new ListNode(1); head.val=0; head.next=p; pp.next=head; return pp; } else if(p==null){ head.next=p; return head; } if(p.val!=9&&f){ p.val+=1; f=false; }else if(p.val==9&&f){ p.val=0; } if(!f&&s){ pre.val-=1; s=false; }else if(f&&s&&head.val>9){ ListNode pp=new ListNode(1); head.val=0; head.next=p; pp.next=head; return pp; } head.next=p; return head; }
import java.util.*; /* * public class ListNode { * int val; * ListNode next = null; * public ListNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @return ListNode类 */ public ListNode plusOne (ListNode head) { // write code here //创建一个集合去存放元素 List<Integer> list = new ArrayList<Integer>(); while(head != null){ list.add(head.val); head = head.next; } ListNode tail = null;//尾结点 int flag = 0 ; //进位 int i = list.size() - 1; while(i >= 0){ if(i == list.size() - 1){ int val = list.get(i) + 1 >= 10 ? 0 : list.get(i) + 1; flag = list.get(i) + 1 >= 10 ? 1 : 0; //同时封装成节点并且进行链表的反转 ListNode temp = new ListNode(val); temp.next = tail; tail = temp; }else{ int val = list.get(i) + flag >= 10 ? 0 : list.get(i) + flag; flag = list.get(i) + flag >= 10 ? 1 : 0; //同时封装成节点并且进行链表的反转 ListNode temp = new ListNode(val); temp.next = tail; tail = temp; } i -- ; } //循环结束后如果还有进位1 if(flag == 1){ ListNode l = new ListNode(1); l.next = tail; tail = l; } return tail; } }
反转以后再做加法
import java.util.*; /* * public class ListNode { * int val; * ListNode next = null; * public ListNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @return ListNode类 */ public ListNode plusOne (ListNode head) { if (head==null) return new ListNode(1); head = reverse(head); ListNode curr = head; ListNode pre=null; int cache = 1; while (curr!=null) { int val = curr.val + cache; curr.val=val%10; cache=val/10; pre=curr; curr=curr.next; } if (cache!=0) pre.next=new ListNode(cache); return reverse(head); } public ListNode reverse(ListNode head) { if (head==null || head.next==null) return head; ListNode pre = null; ListNode curr = head; while (curr!=null) { ListNode tmp = curr.next; curr.next=pre; pre=curr; curr=tmp; } return pre; } }
import java.util.*; /* * public class ListNode { * int val; * ListNode next = null; * public ListNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @return ListNode类 */ public ListNode plusOne (ListNode head) { // write code here head = reverseList(head); ListNode anotherHead = new ListNode(1); return reverseList(addTwoNumbers(head, anotherHead)); } private ListNode addTwoNumbers(ListNode num1, ListNode num2) { ListNode p1 = num1, p2 = num2; int carry = 0; ListNode dummy = new ListNode(-1); ListNode cur = dummy; while(p1 != null || p2 != null){ int sum = carry + (p1 != null? p1.val: 0) + (p2 != null? p2.val: 0); cur.next = new ListNode(sum % 10); carry = sum / 10; if(p1 != null) p1 = p1.next; if(p2 != null) p2 = p2.next; cur = cur.next; } if(carry > 0) cur.next = new ListNode(carry); return dummy.next; } private ListNode reverseList(ListNode head) { ListNode prev = null; ListNode cur = head; while(cur != null){ ListNode next = cur.next; cur.next = prev; prev = cur; cur = next; } return prev; } }