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* reverseEvenLengthGroups(ListNode* head) {
}
};
运行代码
提交
java 解法, 执行用时: 35 ms, 内存消耗: 63.5 MB, 提交时间: 2023-09-07 10:33:27
/**
* 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 reverseEvenLengthGroups(ListNode head) {
List<Integer> nums = new ArrayList<>();
ListNode p = head;
while (p != null) {
nums.add(p.val);
p = p.next;
}
int n = nums.size();
List<Integer> cur_nums;
int cur_len = 1;
ListNode dummy = new ListNode(-1);
p = dummy;
int i = 0;
while (i < n) {
cur_nums = new ArrayList<>(nums.subList(i, Math.min(i + cur_len, n)) );
if (cur_nums.size() % 2 == 0) {
Collections.reverse(cur_nums);
}
for (int x : cur_nums) {
p.next = new ListNode(x);
p = p.next;
}
i += cur_len;
cur_len ++;
}
return dummy.next;
}
}
python3 解法, 执行用时: 1992 ms, 内存消耗: 50.8 MB, 提交时间: 2023-09-07 10:30:34
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseEvenLengthGroups(self, head: Optional[ListNode]) -> Optional[ListNode]:
i = 0
cur, pre = head, None
while cur:
i += 1
it = cur
length = 0
while length < i and it:
length += 1
it = it.next
if length & 1:
for j in range(length):
pre, cur = cur, cur.next
else:
for j in range(length - 1):
pre.next, cur.next.next, cur.next = cur.next, pre.next, cur.next.next
pre, cur = cur, cur.next
return head
golang 解法, 执行用时: 384 ms, 内存消耗: 9.4 MB, 提交时间: 2023-09-07 10:27:01
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func reverseEvenLengthGroups(head *ListNode) *ListNode {
var nodes []*ListNode
for node, size := head, 1; node != nil; node = node.Next {
nodes = append(nodes, node)
if len(nodes) == size || node.Next == nil { // 统计到 size 个节点,或到达链表末尾
if n := len(nodes); n%2 == 0 { // 有偶数个节点
for i := 0; i < n/2; i++ {
nodes[i].Val, nodes[n-1-i].Val = nodes[n-1-i].Val, nodes[i].Val // 直接交换元素值
}
}
nodes = nil
size++
}
}
return head
}