diff --git a/Algorithms_and_Data_Structures/Linked List/segregate_odd_even_valued_nodes/program.py b/Algorithms_and_Data_Structures/Linked List/segregate_odd_even_valued_nodes/program.py new file mode 100644 index 0000000000..8757973bf3 --- /dev/null +++ b/Algorithms_and_Data_Structures/Linked List/segregate_odd_even_valued_nodes/program.py @@ -0,0 +1,62 @@ +class ListNode: + def __init__(self, x): + self.val = x + self.next = None + +class Solution: + def oddEvenList(self, head: ListNode) -> ListNode: + if not head: + return None + + # Initialize odd and even lists + odd_head = ListNode(0) # Dummy head for odd list + even_head = ListNode(0) # Dummy head for even list + + odd = odd_head + even = even_head + current = head + + # Traverse the original list and segregate based on node values + while current: + if current.val % 2 != 0: # Odd valued node + odd.next = current + odd = odd.next + else: # Even valued node + even.next = current + even = even.next + current = current.next + + # Connect odd list to even list + odd.next = even_head.next + even.next = None # Ensure the last node points to None + + return odd_head.next # Return the head of the modified list + +# Helper function to create a linked list from a list +def create_linked_list(arr): + dummy = ListNode(0) + current = dummy + for num in arr: + current.next = ListNode(num) + current = current.next + return dummy.next + +# Helper function to print the linked list +def print_linked_list(head): + values = [] + while head: + values.append(head.val) + head = head.next + print("->".join(map(str, values))) + +# Example Usage +input_list = [1, 2, 3, 4, 5] +head = create_linked_list(input_list) +print("Input Linked List:") +print_linked_list(head) + +solution = Solution() +segregated_head = solution.oddEvenList(head) + +print("Output Linked List:") +print_linked_list(segregated_head) diff --git a/Algorithms_and_Data_Structures/Linked List/segregate_odd_even_valued_nodes/readme.md b/Algorithms_and_Data_Structures/Linked List/segregate_odd_even_valued_nodes/readme.md new file mode 100644 index 0000000000..2bd19d8eb9 --- /dev/null +++ b/Algorithms_and_Data_Structures/Linked List/segregate_odd_even_valued_nodes/readme.md @@ -0,0 +1,33 @@ +# Odd Even Linked List Segregation + +## Problem Statement + +In this problem, you are given a singly linked list of integers. Your task is to segregate the nodes of the linked list based on their values such that all the nodes with odd values come before the nodes with even values, while maintaining the relative order of the odd and even nodes. + +### Example + +**Input:** +1 -> 2 -> 3 -> 4 -> 5 +**Output:** +1 -> 3 -> 5 -> 2 -> 4 + +## Solution Explanation + +The solution involves the following steps: + +1. **Initialization**: + - Create two dummy nodes: one for the odd-valued nodes and one for the even-valued nodes. + - Use two pointers, `odd` and `even`, to keep track of the last nodes in the respective lists. + +2. **Traversal and Segregation**: + - Traverse the original linked list using a pointer `current`. + - For each node, check if its value is odd or even: + - If odd, append it to the odd list. + - If even, append it to the even list. + +3. **Connecting the Lists**: + - After processing all nodes, connect the end of the odd list to the head of the even list. + - Ensure the last node of the even list points to `None`. + +4. **Return the Result**: + - The head of the modified linked list is returned by skipping the dummy node used for odd nodes. diff --git a/Project-Structure.md b/Project-Structure.md index 610c840c86..6fbf1ab99b 100644 --- a/Project-Structure.md +++ b/Project-Structure.md @@ -107,6 +107,8 @@ * [Menu Driven Code For Dynamic Linear Queue Using Linkedlist](Algorithms_and_Data_Structures/Linked%20List/Menu_Driven_Code_for_Dynamic_Linear_Queue_using_LinkedList.py) * [Menu Driven Code For Dynamic Stack Using Linkedlist](Algorithms_and_Data_Structures/Linked%20List/Menu_Driven_Code_for_Dynamic_Stack_using_LinkedList.py) * [Menu Driven Code For Linear Linkedlist](Algorithms_and_Data_Structures/Linked%20List/Menu_Driven_Code_for_Linear_LinkedList.py) + * Segregate Odd Even Valued Nodes + * [Program](Algorithms_and_Data_Structures/Linked%20List/segregate_odd_even_valued_nodes/program.py) * Pattern Search * [Aho Corasick](Algorithms_and_Data_Structures/Pattern%20Search/aho_corasick.py) * [Bitap Algorithm](Algorithms_and_Data_Structures/Pattern%20Search/bitap_algorithm.py)