Odd Even Linked List
Tags: computerscience, 100daysofcode, java
Published: 2020-12-20 21:09:29

Linked list problems can seem tricky at first, but they aren’t so bad once you get the hang of them. Or so they say. I have yet to get to that point, which is why I am here blogging about it! If you are brand new to linked lists then check out this [post](https://www.danielleskosky.com/linked-lists/). While, [reversing linked lists](https://leetcode.com/problems/reverse-linked-list/) and [finding cycles](https://leetcode.com/problems/linked-list-cycle/) are great problems to start on, this odd even linked list problem is a pretty good one for us beginners as well. Let’s get started!

## The Problem

Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.

[Link to Problem](https://leetcode.com/problems/odd-even-linked-list/)

Check out Figure A for an example input and result.

![Figure A](https://cdn-images-1.medium.com/max/2000/1*-ZkImL_Ec4-7ZHEH-XLr-w.png)<center>*Figure A*</center>

## How to Solve

For this problem, we are going to create two linked lists. One of the linked lists will contain all of the odd nodes from the original list and the other list will contain all of the even nodes from the original list. Once we have these two lists, the even list will get added on to the end of the odd list.

In this example the list nodes just happen to have values that correspond to their placement in the list. Just a reminder that the problem states that “we are talking about the node number and not the value in the nodes”.

## The Code

Let’s take a look at the code to solve this problem. Read through the code and make as much sense of it as possible. Then we can go through it step-by-step and make sure that we understand it completely.

{% gist
https://gist.github.com/djl218/3e7f0b26536d9bc0a08185241812cbe0 %}

Here are the steps that the algorithm takes:

1. Create 5 new nodes: oddHead, evenHead, oddNode, evenNode, and current.

1. oddHead and evenHead do not move during this algorithm. They will keep track of the beginning of the odd and even lists.

1. Current iterates through the nodes of the list until it reaches null. At each iteration of current, the node that current points to will either be connected to the end of the odd list or the even list.

1. Once current reaches null, the even list will get connected to the end of the odd list.

## Some Illustrations

I know what might be helpful! How about we use some illustrations to help us better understand what is going on in the algorithm.

Take a look at Figure B. It shows what happens in lines 15–21 of the code. This can be thought of as a preparatory step. It is common for linked list problems to have a preparatory step like this. I also want to mention that head is still pointing to the first node. It just isn’t shown to make things a bit clearer.

![Figure B](https://cdn-images-1.medium.com/max/2000/1*S8EDHoCLkVHiYQDPjRSoyQ.png)<center>*Figure B*</center>

Figure C shows what happens in lines 23–25 of the code. oddNode.next is set to current, current points to the next node, and oddNode now points to where current just was. Also notice that oddHead stays in the same spot.

![Figure C](https://cdn-images-1.medium.com/max/2000/1*fNT2FXU90wg1REV5OrN-Tw.png)<center>*Figure C*</center>

Figure D show what happens in lines 27–29 of the code. evenNode.next is set to current, current points to the next node, and evenNode now points to where current just was. Once again, notice that evenHead stays in the same spot.

![Figure D](https://cdn-images-1.medium.com/max/2000/1*5MTYxEAgqdZ67wlrAa16pA.png)<center>*Figure D*</center>

In Figure E we are back to lines 23–25 of the code. oddNode.next is set to current, current points to the next node, and oddNode now points to where current just was. Take note that current now points to NULL.

![Figure E](https://cdn-images-1.medium.com/max/2000/1*8KkQBpvDO_xX5ByCtYWg7w.png)<center>*Figure E*</center>

In Figure F only lines 27–28 of the code are applied because current = NULL. evenNode.next is set to current and evenNode points to where current is. current = NULL and the while loop terminates.

![Figure F](https://cdn-images-1.medium.com/max/2000/1*9hhC11CinK4yOZg8kceDMw.png)<center>*Figure F*</center>

Now on to line 32 of the code. oddNode.next is set to evenHead, which results in the even list being added to the end of the odd list. The original head has been brought back just in case you forgot it was still there.

![Figure G](https://cdn-images-1.medium.com/max/2000/1*V0cRS0wd4vgnrGn-tifsIA.png)<center>*Figure G*</center>

**Output:**

```code    
result: [1, 3, 5, 2, 4]
```

## Time Complexity

This algorithm will have a time complexity of O(n) with n being the number of nodes in the linked list.

## Space Complexity

The algorithm runs in constant space O(1).

Want to know more about Big-O? I know of a great place to [start](https://www.danielleskosky.com/big-o/).

## Get Better at Algorithms!

Algorithms and data structures are pretty tough. They are definitely taking a while for me to get the hang of them. However, there are some great resources out there, and I feel obligated to share some that have been most helpful to me. If I missed any that have been helpful to you, be sure to mention them in the comments.

* **Cracking the Coding Interview** — Great resource. Really gets you in the right mindset for interviews. You can find it [here](https://www.amazon.com/Cracking-Coding-Interview-Programming-Questions/dp/0984782850).

* **Elements of Programming Interviews** — Another great book. Personally, I like this one more than CTCI, but YMMV. You can find it [here](https://www.amazon.com/Elements-Programming-Interviews-Java-Insiders/dp/1517671272/ref=pd_lpo_14_t_0/134-2745636-3821839?_encoding=UTF8&pd_rd_i=1517671272&pd_rd_r=4eebd030-1368-436b-9bdd-22a403a57eae&pd_rd_w=ku4HH&pd_rd_wg=qY5q5&pf_rd_p=7b36d496-f366-4631-94d3-61b87b52511b&pf_rd_r=Y44KR67YH071M3P9JGSH&psc=1&refRID=Y44KR67YH071M3P9JGSH).

* **Grokking the Coding Interview** — Can’t emphasize this one enough. Haven’t seen it mentioned it too often. Explains patterns that occur in different coding challenge problems. Great at providing a big-picture of all the different algorithm problems you might encounter. Check it out [here](https://www.educative.io/courses/grokking-the-coding-interview).

* **Back To Back SWE** — Great [YouTube Channel](https://www.youtube.com/channel/UCmJz2DV1a3yfgrR7GqRtUUA). Highly recommend.

* **Kevin Naughton Jr.** — Another awesome [YouTube channel](https://www.youtube.com/channel/UCKvwPt6BifPP54yzH99ff1g). Great at going over problems and gives helpful advice.

* **Base CS** — Vaidehi Joshi does a great job of laying out the fundamentals of algorithms and data structures. Check out the blog series [here](https://medium.com/basecs). She also has a [podcast](https://www.codenewbie.org/basecs) that I give two thumbs up.

* **Coding Challenge Website** — There are plenty of different ones to choose from. [HackerRank](https://www.hackerrank.com/), [CodeWars](https://www.codewars.com/), and [Edabit](https://edabit.com/) all seem to be pretty popular. I personally use [LeetCode](https://leetcode.com/). Find the one that works for you!

* **Pramp** — Do mock interviews! The sooner the better! [Pramp](https://www.pramp.com/) has been a huge help to me.

Well, I hope that was useful. Thanks for reading my post and best of luck with your learning about data structures and algorithms!