forked from UTSAVS26/PyVerse
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request UTSAVS26#1024 from SKG24/main
Added UTSAVS26#1014 | Segregate Even and Odd node | Linked List
- Loading branch information
Showing
3 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
Algorithms_and_Data_Structures/Linked List/segregate_odd_even_valued_nodes/program.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
33 changes: 33 additions & 0 deletions
33
...ithms_and_Data_Structures/Linked List/segregate_odd_even_valued_nodes/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters