odd_even_linked_list

  • 2022-12-14
  • 浏览 (543)

odd_even_linked_list.py 源码

# 奇偶链表


class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


class Solution:
    def oddEvenList(self, head: ListNode) -> ListNode:
        if not head:
            return head

        evenHead = head.next
        odd, even = head, head.next
        while even and even.next:
            odd.next, odd = even.next, even.next
            even.next, even = odd.next, odd.next
        odd.next = evenHead
        return head

你可能感兴趣的文章

add_two_numbers

delete_nth_node

intersection_of_two_linked_lists

0  赞