Solving Linked List Problems | LeetCode 203 & 206

Over the past week I have been trying to learn new data structures and understand how they are implemented to solve everyday issues. One way I have been able to practice and implement linked lists is through LeetCode. During the week I found problems 203 Remove Linked List Elements and 206 Reverse Linked List to be the most useful problems for beginners getting a grasp on the basic concept of Linked Lists.
Problem 203. Remove Linked List Elements
Let’s start by reading through the requirements of solving the problem.

The first test case we must have if we are going to solve this problem recursively is to check if the current head is null if so the function will return null. Now next the function will check if the value of the head is the same as the passed in value, if so then we then we call the function again but pass in head.next as the node. If neither case is true then the next value of the head will be the next value and the head will be removed. This passes all the test cases with a runtime of 84ms and memory usage of 39.5MB.
Problem 206. Reverse Linked List
Next let’s look at the requirements for the next problem.

To solve the problem we will use a while loop and a dummy node to keep track of the latest node in the list. So lets start by creating a dummy variable called last and setting to null. Next we set up a while loop to test if the head node is not null, if it is true then we create a temporary variable set to head.next and set head.next to last and last to head. This will repeat and cause the list to reverse itself until we reach the last node in the list. This passed all the test cases with a runtime of 84ms and memory usage of 36.3MB.