
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Odd Even Linked List in Python
Suppose we have a singly linked list, we have to group all odd nodes together followed by the even nodes. Here we are talking about the node position not the value in the nodes. We should try to do it in place. So if the nodes are [1,22,13,14,25], the result will be [1,13,25,22,14]
To solve this, we will follow these steps −
- if head is null or the next of head is null, then return head
- head1 := head, head2 := next of head, head_beg := next of head
- while next of head2 is nor null and next of (next of head is not null)
- next of head1 := next of head2
- next of head2 = next of (next of head)
- head1 := next of head1 and head2 := next of head2
- if next of head2 is not null
- next of head1 := next of head2
- head1 := next of head1
- next of head1 := head2_beg and next of head2 = null
- return head
Let us see the following implementation to get better understanding −
Example
class ListNode: def __init__(self, data, next = None): self.val = data self.next = next def make_list(elements): head = ListNode(elements[0]) for element in elements[1:]: ptr = head while ptr.next: ptr = ptr.next ptr.next = ListNode(element) return head def print_list(head): ptr = head print('[', end = "") while ptr: print(ptr.val, end = ", ") ptr = ptr.next print(']') class Solution(object): def oddEvenList(self, head): if head == None or head.next ==None: return head head1=head head2,head2_beg= head.next,head.next while head2.next!= None and head2.next.next!= None: head1.next = head2.next head2.next = head2.next.next head1 = head1.next head2 = head2.next if head2.next!=None: head1.next = head2.next head1 = head1.next head1.next = head2_beg head2.next = None return head ob1 = Solution() head = make_list([1,22,13,14,25]) print_list(ob1.oddEvenList(head))
Input
[1,22,13,14,25]
Output
[1, 13, 25, 22, 14, ]
- Related Articles
- Check whether the length of given linked list is Even or Odd in Python
- Alternate Odd and Even Nodes in a Singly Linked List in C++
- Find sum of even and odd nodes in a linked list in C++
- Python program to Count Even and Odd numbers in a List
- Python Program to Put Even and Odd elements in a List into Two Different Lists
- Palindrome Linked List in Python
- Reverse Linked List in Python
- Linked List Cycle in Python
- Program to find linked list intersection from two linked list in Python
- Python Program for Odd-Even Sort / Brick Sort
- Use of :even and :odd pseudo-classes with list items in CSS
- Even numbers at even index and odd numbers at odd index in C++
- Delete all the even nodes from a Doubly Linked List in C++
- Swap Even Index Elements And Odd Index Elements in Python
- Odd Even Jump in C++

Advertisements