LeetCode 141 Linked List Cycle in Java

In this post we will solve the LeetCode 141 Linked List Cycle problem.

Given head, the head of a linked list, 
determine if the linked list has a cycle in it.

There is a cycle in a linked list 
if there is some node in the list that can be reached again by continuously following the next pointer. 

Internally, pos is used to denote the index of the node that tail's next pointer is connected to. 
Note that pos is not passed as a parameter.

Return true if there is a cycle in the linked list. 
Otherwise, return false.

Constraints:

o The number of the nodes in the list is in the range [0, 10^4].
o -10^5 <= Node.val <= 10^5
o pos is -1 or a valid index in the linked-list.

Related Topics:

o Hash Table
o Linked List
o Two Pointers

We are given a singly linked list and are asked to determine if there is a cycle in it. If interested in the problem please take a look at the current description of it in the LeetCode website. Continue reading “LeetCode 141 Linked List Cycle in Java”

LeetCode 328 Odd Even Linked List in Java

In this post I will try three implementations of the function of interest for LeetCode 328 Odd Even Linked List problem using the Java programming language.

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!!!

You should try to do it in place. 
The program should run in O(1) space complexity and O(nodes) time complexity.

Constraints:

o The relative order inside both the even and odd groups should remain as it was in the input.
o The first node is considered odd, the second node even and so on ...
o The length of the linked list is between [0, 10^4].

We are given a singly linked list. We need to group all `odd` nodes together followed by the `even` nodes. Please note the definition of what makes a node `odd` and `even`. It is not the value in the node but the position of the node in the linked list. Continue reading “LeetCode 328 Odd Even Linked List in Java”

LeetCode 237. Delete Node in a Linked List in Java

I noticed that a second implementation of the function of interest was missing from this post. I have updated the GitHub repo and the post. Sorry about that!

In this post we will solve the LeetCode 237 Delete Node in a Linked List problem using the Java programming language and the VSCode IDE on a Windows platform. Java is quite portable so it should run on most (never generalize) platforms.

Write a function to delete a node in a singly-linked list. 
You will not be given access to the head of the list, 
instead you will be given access to the node to be deleted directly.

It is guaranteed that the node to be deleted is not a tail node in the list.

Constraints:

o The number of the nodes in the given list is in the range [2, 1000].
o -1000 &amp;lt;= Node.val &amp;lt;= 1000
o The value of each node in the list is unique.
o The node to be deleted is in the list and is not a tail node

The problem at first glance seems to be simple. We have to delete a node from a singly linked list. The complication arises when we are only given the node to delete. In general, to delete a node from a linked list we are given the head of the linked list. Continue reading “LeetCode 237. Delete Node in a Linked List in Java”

Middle of the Linked List

I am working on the last 2-hour block of the day. My wife was trying to make a payment for our Internet service, but she needs a number that can only be found in a previous bill receipt. Apparently she can not find one, so at the end of the workday we will go to make a payment in person and will hopefully keep the receipt, so this situation does not repeat. Continue reading “Middle of the Linked List”

Add Two Numbers in Java

Hope your day is going well. I have not checked the mail box in a few days. I will have to do so by the end of the workday. In this day and age of on-line communications it seems that due to security and privacy companies continue to send information and documents via USPS.

I was looking at a LeetCode problem to solve next when I found LeetCode 2. Add Two Numbers. The title for the problem and the description sounded quite similar to the problem Add Strings which we solved a day or so ago. I decided to give it a try. Hopefully we will use some of the same techniques we used to solve the last one. Continue reading “Add Two Numbers in Java”

Reverse Linked List

In this post I will reverse a linked list in Java. As I mentioned in a previous post, I am in the process of refreshing recursion using Java. In the next few days I will pick up a few more problems and post my approach.

In this post I will deal with 206. Reverse Linked List. If interested please take a look at the requirements for the problem and then give it a try before looking at solutions. Continue reading “Reverse Linked List”

Is Unique 3

Today is Thursday, one more day to go. The nice thing is that this coming weekend is Labor Day.

As I mentioned in a previous post, I received my Azure Kinect DK camera. I checked the hardware requirements and tried matching to my machines at home. I decided to get a Windows base pedestal instead of a Linux server. The hardware requirements for the computer follow:

System Requirements: Windows® 10 PC or Ubuntu 18.04 LTS with 7th Generation Intel® CoreTM i3 Processor (Dual Core 2.4 GHz with HD620 GPU or faster), USB 3.0 Port, 4 GB RAM. Not available on Windows 10 in S mode. Skeletal/body tracking and other experiences may require more advanced PC hardware. Continue reading “Is Unique 3”