Understanding the LeetCode Problem: Splitting a Linked List by Parity
Are you ready to tackle a challenging problem on LeetCode? If you’re looking to enhance your skills in data structures and algorithms, the “Splitting a Linked List by Parity” problem is a great choice. In this article, I’ll guide you through the problem, its requirements, and provide a detailed solution. Let’s dive in!
Problem Description
The “Splitting a Linked List by Parity” problem on LeetCode requires you to write a function that takes a linked list as input and splits it into two separate lists: one containing all even-valued nodes and the other containing all odd-valued nodes. The order of the nodes within each list should be preserved, and the function should return the head of the even-valued list.
Understanding the Linked List
Before we proceed, let’s clarify what a linked list is. A linked list is a linear data structure where each element is a separate object called a node. Each node contains a value and a reference (or link) to the next node in the sequence. In this problem, we’ll be working with a singly linked list, which means each node has only one reference to the next node.
Here’s an example of a linked list with even and odd values:
1 -> 2 -> 3 -> 4 -> 5
In this list, nodes with even values are 2, 4, and 5, while nodes with odd values are 1 and 3.
Approach to Solve the Problem
There are multiple ways to solve this problem, but one of the most efficient approaches is to use two pointers. The idea is to create two separate lists: one for even values and one for odd values. We’ll iterate through the original list, and for each node, we’ll check if its value is even or odd. Based on the result, we’ll append the node to the appropriate list.
Implementation
Let’s implement the solution in Python. We’ll define a class for the linked list node and then create a function to split the list by parity.
class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = nextdef partition(head): even_head = ListNode(0) odd_head = ListNode(0) even = even_head odd = odd_head while head: if head.val % 2 == 0: even.next = head even = even.next else: odd.next = head odd = odd.next head = head.next odd.next = None even.next = odd_head.next return even_head.next
Explanation of the Code
In the code above, we first create two dummy nodes, `even_head` and `odd_head`, to serve as the starting points for our even and odd lists. We also initialize two pointers, `even` and `odd`, to these dummy nodes.
Next, we iterate through the original list using a while loop. For each node, we check if its value is even or odd. If it’s even, we append it to the even list by updating the `next` reference of the `even` pointer. If it’s odd, we append it to the odd list by updating the `next` reference of the `odd` pointer.
After iterating through the entire list, we set the `next` reference of the `odd` pointer to `None` to ensure that the odd list is terminated. Finally, we connect the even list to the odd list by setting the `next` reference of the `even` pointer to the `next` reference of the `odd_head` node. We return the `next` reference of the `even_head` node, which is the head of the even list.
Testing the Solution
Now that we have our solution implemented, let’s test it with a sample input:
def print_list(node): while node: print(node.val, end=" ") node = node.next Create a linked list: 1 -> 2 -> 3 -> 4 -> 5head = ListNode(1, ListNode(2, ListNode(3, ListNode(4, ListNode(5))))) Split the list by paritysplit_head = partition(head) Print the even and odd listsprint("Even list:")print_list(split_head)print("Odd list:")print_list(