C++
Java
Python
Python3
C
C#
JavaScript
Ruby
Swift
Go
Scala
Kotlin
Rust
PHP
TypeScript
Racket
Erlang
Elixir
Dart
monokai
ambiance
chaos
chrome
cloud9_day
cloud9_night
cloud9_night_low_color
clouds
clouds_midnight
cobalt
crimson_editor
dawn
dracula
dreamweaver
eclipse
github
github_dark
gob
gruvbox
gruvbox_dark_hard
gruvbox_light_hard
idle_fingers
iplastic
katzenmilch
kr_theme
kuroir
merbivore
merbivore_soft
mono_industrial
nord_dark
one_dark
pastel_on_dark
solarized_dark
solarized_light
sqlserver
terminal
textmate
tomorrow
tomorrow_night
tomorrow_night_blue
tomorrow_night_bright
tomorrow_night_eighties
twilight
vibrant_ink
xcode
上次编辑到这里,代码来自缓存 点击恢复默认模板
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* doubleIt(ListNode* head) {
}
};
运行代码
提交
javascript 解法, 执行用时: 168 ms, 内存消耗: 50.9 MB, 提交时间: 2023-08-14 09:38:54
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var doubleIt = function(head) {
if (head.val > 4)
head = new ListNode(0, head);
for (var cur = head; cur != null; cur = cur.next) {
cur.val = cur.val * 2 % 10;
if (cur.next != null && cur.next.val > 4)
cur.val++;
}
return head;
};
java 解法, 执行用时: 2 ms, 内存消耗: 43.8 MB, 提交时间: 2023-08-14 09:37:47
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode doubleIt(ListNode head) {
if (head.val > 4)
head = new ListNode(0, head);
for (var cur = head; cur != null; cur = cur.next) {
cur.val = cur.val * 2 % 10;
if (cur.next != null && cur.next.val > 4)
cur.val++;
}
return head;
}
}
cpp 解法, 执行用时: 300 ms, 内存消耗: 113.3 MB, 提交时间: 2023-08-14 09:37:24
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode *doubleIt(ListNode *head) {
if (head->val > 4)
head = new ListNode(0, head);
for (auto cur = head; cur; cur = cur->next) {
cur->val = cur->val * 2 % 10;
if (cur->next && cur->next->val > 4)
cur->val++;
}
return head;
}
};
golang 解法, 执行用时: 88 ms, 内存消耗: 7 MB, 提交时间: 2023-08-14 09:36:43
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func doubleIt(head *ListNode) *ListNode {
if head.Val > 4 {
head = &ListNode{0, head}
}
for cur := head; cur != nil; cur = cur.Next {
cur.Val = cur.Val * 2 % 10
if cur.Next != nil && cur.Next.Val > 4 {
cur.Val++
}
}
return head
}
python3 解法, 执行用时: 312 ms, 内存消耗: 19.6 MB, 提交时间: 2023-08-14 09:35:55
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
'''
不考虑进位,则每个节点*2;考虑进位,只有当节点值大于4会进位,进位则加一个节点。
'''
class Solution:
def doubleIt(self, head: Optional[ListNode]) -> Optional[ListNode]:
if head.val > 4:
head = ListNode(0, head)
cur = head
while cur:
cur.val = cur.val * 2 % 10
if cur.next and cur.next.val > 4:
cur.val += 1
cur = cur.next
return head
golang 解法, 执行用时: 72 ms, 内存消耗: 7.2 MB, 提交时间: 2023-08-14 09:34:36
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
// 206. 反转链表
// 视频讲解 https://www.bilibili.com/video/BV1sd4y1x7KN/
func reverseList(head *ListNode) *ListNode {
var pre, cur *ListNode = nil, head
for cur != nil {
nxt := cur.Next
cur.Next = pre
pre = cur
cur = nxt
}
return pre
}
// 2. 两数相加:自己和自己相加
// 题解 https://leetcode.cn/problems/add-two-numbers/solution/dong-hua-jian-ji-xie-fa-cong-di-gui-dao-oe0di/
func double(l1 *ListNode) *ListNode {
dummy := &ListNode{} // 哨兵节点,作为新链表的头节点的前一个节点
cur := dummy
carry := 0 // 进位
for l1 != nil {
carry += l1.Val * 2 // 节点值和进位加在一起
cur.Next = &ListNode{Val: carry % 10} // 每个节点保存一个数位
carry /= 10 // 新的进位
cur = cur.Next // 下一个节点
l1 = l1.Next // 下一个节点
}
if carry != 0 {
cur.Next = &ListNode{Val: carry}
}
return dummy.Next
}
func doubleIt(head *ListNode) *ListNode {
head = reverseList(head)
res := double(head) // 反转后,就变成【2. 两数相加】了
return reverseList(res)
}
python3 解法, 执行用时: 516 ms, 内存消耗: 21.6 MB, 提交时间: 2023-08-14 09:33:51
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
# 看做是两个head相加
# https://space.bilibili.com/206214
class Solution:
# 206. 反转链表
# 视频讲解 https://www.bilibili.com/video/BV1sd4y1x7KN/
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
pre = None
cur = head
while cur:
nxt = cur.next
cur.next = pre
pre = cur
cur = nxt
return pre
# 2. 两数相加:自己和自己相加
# 题解 https://leetcode.cn/problems/add-two-numbers/solution/dong-hua-jian-ji-xie-fa-cong-di-gui-dao-oe0di/
def double(self, l1: Optional[ListNode]) -> Optional[ListNode]:
cur = dummy = ListNode() # 哨兵节点
carry = 0 # 进位
while l1: # 有一个不是空节点,或者还有进位,就继续迭代
carry += l1.val * 2 # 节点值和进位加在一起
cur.next = ListNode(carry % 10) # 每个节点保存一个数位
carry //= 10 # 新的进位
cur = cur.next # 下一个节点
l1 = l1.next # 下一个节点
if carry:
cur.next = ListNode(carry)
return dummy.next # 哨兵节点的下一个节点就是头节点
def doubleIt(self, head: Optional[ListNode]) -> Optional[ListNode]:
head = self.reverseList(head)
res = self.double(head) # 反转后,就变成【2. 两数相加】了
return self.reverseList(res)