Create group_odd_and_even.py

This commit is contained in:
marina 2023-08-02 19:19:54 -07:00 committed by GitHub
parent e085d74110
commit e5c9084b64
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -0,0 +1,31 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# author: bt3gl
class Node:
def __init__(self, val):
self.val = val
self.next = None
def group_odd_and_even(head):
if not head:
return None
odd = head
even = odd.next
even_head = even
while even 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 = even_head
return head