Algorithmic problems: Odd Even Linked List

Algorithmic problems: Odd Even Linked List

ยท

3 min read

The sixth problem https://leetcode.com/problems/odd-even-linked-list/ of the December leetcoding challenge is a Medium category which tests our linked list iteration skills. Honestly comping up with the naive implementation of the problem was easy but I tripped a little while optimizing it.

Problem breakdown

We are given the reference to the head pointer of a linked list and asked to group the odd indices / nodes together and the even nodes together then return the modified list. While doing so, the relative order of the nodes should also be preserved. As usual, I started with a simple approach to solve it so taking an extra space such as another array, one array for storing the odd node values and the other for storing the even node values. After doing so, creating a new linked list using those values is trivial.

However, the obvious bottleneck here is the requirement for extra space and we need a smarter way to get our job done without using extra spaces. A side fact - I got confused by looking the no extra space constraint and thought we need to modify the linked list in place like we do in case of arrays and ended up wasting a lot of time going no where ๐Ÿ˜ข. Since we can create linked lists from pointers alone, this does not violate the extra space constraint.

My approach

We will create two dummy nodes one for the odds and the other for even nodes called evens. We will also take extra two pointers oddList and evenList to keep a reference to their heads. Now just like we would do with the extra space we will initialize a counter to keep track of the odds and even nodes. We iterate through the linked list and at each iteration we

  • check if the current node is odd/even

  • insert the node as the next node to the odds if the current node is an odd indices and vice versa

  • increment the pointer to advance forward in the odds and evens lists

  • increment the pointer to advance forward in the original given list

  • increment counter

  • finally after the iteration is over,

Since we are to return the list odds first and evens last we

  • indicate that the list is over by making the tail of the evens list to null and the tail of the odds list point to the head of the evenList .

    This was the reason, we took extra two pointers to point to the heads of the newly created lists.

def oddEvenList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        oddList = odds = ListNode(0)
        evenList = evens = ListNode(0)
        count = 1
        while head:
            if count % 2:
                odds.next = head
                odds = odds.next
            else:
                evens.next = head
                evens = evens.next
            count += 1
            head = head.next
        evens.next = None
        odds.next = evenList.next

        return oddList.next

Time and Space complexity

No extra spaces used so space complexity is O(1) and we iterate once through the given list and perform the checks and assignment operations, so time complexity is O(n) .

Phew! That took some brain juice. Let me know how you overcame this problem. Until next post.

ย