From c0b72b681468520750cfea28f8bc2a58133e9524 Mon Sep 17 00:00:00 2001 From: jaredliw Date: Wed, 14 Jul 2021 11:08:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0py=EF=BC=8C=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../链表篇/leetcode328奇偶链表.md | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/animation-simulation/链表篇/leetcode328奇偶链表.md b/animation-simulation/链表篇/leetcode328奇偶链表.md index 83f4cd9..85bede3 100644 --- a/animation-simulation/链表篇/leetcode328奇偶链表.md +++ b/animation-simulation/链表篇/leetcode328奇偶链表.md @@ -46,7 +46,7 @@ class Solution { } ListNode odd = head; ListNode even = head.next; - ListNode evenhead = even; + ListNode evenHead = even; while (odd.next != null && even.next != null) { //将偶数位合在一起,奇数位合在一起 @@ -56,7 +56,7 @@ class Solution { even = even.next; } //链接 - odd.next = evenhead; + odd.next = evenHead; return head; } } @@ -73,7 +73,7 @@ public: } ListNode * odd = head; ListNode * even = head->next; - ListNode * evenhead = even; + ListNode * evenHead = even; while (odd->next != nullptr && even->next != nullptr) { //将偶数位合在一起,奇数位合在一起 @@ -83,7 +83,7 @@ public: even = even->next; } //链接 - odd->next = evenhead; + odd->next = evenHead; return head; } }; @@ -95,13 +95,36 @@ var oddEvenList = function(head) { if(!head || !head.next) return head; let odd = head, even = head.next, evenHead = even; while(odd.next && even.next){ + //将偶数位合在一起,奇数位合在一起 odd.next = even.next; odd = odd.next; even.next = odd.next; even = even.next; } + //链接 odd.next = evenHead; return head; }; ``` +Python Code: + +```py +class Solution: + def oddEvenList(self, head: ListNode) -> ListNode: + if head is None or head.next is None: + return head + odd = head + even = head.next + evenHead = even + while odd.next is not None and even.next is not None: + # 将偶数位合在一起,奇数位合在一起 + odd.next = even.next + odd = odd.next + even.next = odd.next + even = even.next + # 链接 + odd.next = evenHead + return head +``` +