Leetcode 369. Plue 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.

Example:

Input:
1->2->3

Output:
1->2->4

思路: 题目给的正向List无法处理最后一个node加一后的进位问题。所以思路是将List reverse过来,加一,然后在reverse一次。

注意L12到L14,这里需要将node.val先存一下,否则L13会将这个值破坏,导致node.val = 9时变得carry本应是1,结果输出0。

 1 class Solution(object):
 2     def plusOne(self, head):
 3         """
 4         :type head: ListNode
 5         :rtype: ListNode
 6         """
 7         end = self.reverseList(head)
 8         carry = 1
 9         start = end
10         prev = ListNode(0)
11         while start:
12             cur = start.val
13             start.val = (cur + carry)%10
14             carry = (cur + carry)//10
15
16             prev = start
17             start = start.next
18
19         if carry == 1:
20             prev.next = ListNode(1)
21
22         return self.reverseList(end)
23
24
25
26     def reverseList(self, head):
27         prev = None
28
29         while head:
30             temp = head.next
31             head.next = prev
32             prev = head
33             head = temp
34
35         return prev
时间: 2024-10-25 14:12:44

Leetcode 369. Plue one Linked List的相关文章

369. Plus One Linked List

/* * 369. Plus One Linked List * 2016-7-16 by Mingyang * 直接先reverse,做完了再reverse,屌丝代码,未优化,就这么样吧,哈哈 */ public static ListNode plusOne(ListNode head) { if(head==null) return null; head=reverseList(head); ListNode pre=new ListNode(-1); pre.next=head; Lis

LeetCode 206 链表 Reverse Linked List

LeetCode 206 链表 Reverse Linked List Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL Follow up: A linked list can be reversed either iteratively or recursively. Could you implement

[LeetCode][JavaScript]Odd Even Linked List

Odd Even Linked List Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes. You should try to do it in place. The program should run i

leetcode Intersection of Two Linked Lists python

Intersection of Two Linked Lists Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. python

【Leetcode】 Odd Even Linked List

题目链接:https://leetcode.com/problems/odd-even-linked-list/ 题目: Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes. You should try to

LeetCode刷题:Linked List Cycle 及其进阶Linked List Cycle II

Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 1. 首先解决的问题是 判断单链表 有没有环? 解题思路: 两个指针slow 和 fast,开始都指向 head, slow指针每次走一步,fast 指针每次走两步,看看最终 slow 和 fast 会不会重合,如果 链表有环,那么一定会重合. /** * Definition

LeetCode OJ 92. Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the following condition:1 ≤ m ≤ n ≤ lengt

leetcode:Odd Even Linked List

Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes. You should try to do it in place. The program should run in O(1) space complexi

LeetCode OJ :Remove Linked List Elements (移除链表元素)

Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5 ps:这一题感觉没什么技巧可言,选取一个头指针,一个当前指针,一个前向指针.简单的链表操作而已,代码如下: 1 /**