Odd Even Linked List

In this tutorial, I am going to discuss a programming question how to segregate even and odd nodes in a linked list or odd even linked list.

Given a singly linked list, We have to write a code to group all odd nodes together followed by the even nodes. We have to group based on node number not the value present in the node.

Try to solve this problem in O(n) time complexity and O(1) space complexity where n is the no. of nodes in a linked list.

Also, the relative order inside both the even and odd groups should remain as it was in the input. The first node is considered odd, the second node even and so on …

For example –

In the this example, Given input linked list is

1 -> 2 -> 3 -> 4 -> NULL

When we segregate even and odd nodes in a Linked List then the output is

1 -> 3 -> 2 -> 4 -> NULL

Check If a String is Subsequence of Another String

In this tutorial, I am going to discuss a problem Is Subsequence (check if a string is subsequence of another string).

Given two strings str1 and str2, check if str1 is a subsequence of str2. Both strings consists only of lowercase characters.

Example 1:

Input: str1 = “abc”, str2 = “ahbgdc”

Output: true

Example 2:

Input: s = “axc”, t = “ahbgdc”

Output: false

Valid Perfect Square

How to check valid perfect square without sqrt (in-built Library function).

Given a positive integer, We have to write a code to returns true if it is a perfect square else false.

For solving this problem, we don’t have to use any built-in library function (such as sqrt).

For Example –

Example 1:

Input:  36

output: true

Example 2:

Input:  17

output: false

Example 3:

Input:  1

output: true