Skip to content

Commit

Permalink
Merge pull request UTSAVS26#1024 from SKG24/main
Browse files Browse the repository at this point in the history
Added UTSAVS26#1014 | Segregate Even and Odd node | Linked List
  • Loading branch information
UTSAVS26 authored Nov 3, 2024
2 parents cddca8f + 0aa64e1 commit a9f8c45
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 2 additions & 0 deletions Project-Structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a9f8c45

Please sign in to comment.