C++
Java
Python
Python3
C
C#
JavaScript
TypeScript
PHP
Swift
Kotlin
Dart
Go
Ruby
Scala
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:
vector<ListNode*> splitCircularLinkedList(ListNode* list) {
}
};
运行代码
提交
golang 解法, 执行用时: 172 ms, 内存消耗: 9.6 MB, 提交时间: 2023-10-16 21:23:35
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func splitCircularLinkedList(list *ListNode) []*ListNode {
fast, slow := list, list
for fast.Next != list && fast.Next.Next != list {
slow = slow.Next
fast = fast.Next.Next
}
t := slow.Next
if fast.Next == list {
fast.Next = t
}
if fast.Next.Next == list {
fast.Next.Next = t
}
slow.Next = list
return []*ListNode{list, t}
}
java 解法, 执行用时: 2 ms, 内存消耗: 55.8 MB, 提交时间: 2023-10-16 21:20:34
/**
* 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[] splitCircularLinkedList(ListNode list) {
ListNode cur1 = list;
ListNode cur2 = list;
while (cur2.next != list && cur2.next.next != list) {
cur1 = cur1.next;
cur2 = cur2.next.next;
}
if (cur2.next != list) cur2 = cur2.next;
ListNode temp = cur1.next;
cur1.next = list;
cur2.next = temp;
return new ListNode[]{list, temp};
}
}
cpp 解法, 执行用时: 488 ms, 内存消耗: 201.3 MB, 提交时间: 2023-10-16 21:14:35
/**
* 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:
vector<ListNode*> splitCircularLinkedList(ListNode* list) {
ListNode* cur1 = list;
ListNode* cur2 = list;
while (cur2->next != list and cur2->next->next != list) {
cur1 = cur1->next;
cur2 = cur2->next->next;
}
if (cur2->next != list) cur2 = cur2->next;
auto temp = cur1->next;
cur1->next = list;
cur2->next = temp;
return {list, temp};
}
};
python3 解法, 执行用时: 848 ms, 内存消耗: 50 MB, 提交时间: 2023-10-16 21:14:00
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def splitCircularLinkedList(self, head: Optional[ListNode]) -> List[Optional[ListNode]]:
fast = slow = head
while fast.next != head and fast.next.next != head:
slow = slow.next
fast = fast.next.next
t = slow.next
if fast.next == head: fast.next = t
if fast.next.next == head: fast.next.next = t
slow.next = head
return [head, t]
python3 解法, 执行用时: 744 ms, 内存消耗: 50.1 MB, 提交时间: 2023-10-16 21:13:36
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def splitCircularLinkedList(self, list: Optional[ListNode]) -> List[Optional[ListNode]]:
p1,p2,lst,f = list,list,list,1
while p2.next != list:
lst,p1 = p1,p1.next
if p2.next.next != list:
p2 = p2.next.next
f += 2
else:
p2 = p2.next
f += 1
mid = p1 if f & 1 else lst
q = mid.next
mid.next = list
p2.next = q
return [list,q]