mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 20:26:07 -04:00
Create group_odd_and_even.py
This commit is contained in:
parent
e085d74110
commit
e5c9084b64
31
linked_lists/group_odd_and_even.py
Normal file
31
linked_lists/group_odd_and_even.py
Normal 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
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user