# Category Archives: Linked List

View algorithms on Linked Lists.

## Find cycle in a linked list. (Method 2)

Question: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Output: 0 The easier method to find the cycle has been discussed in this post. This method is based upon the idea of a circular race track. If there is a slow runner and a fast runner, eventually the… Read More »

## Find cycle in a linked list. (Method 1)

Question: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Output: 0 We are given a linked list which may or may not have a loop. A loop in a linked list means that if we keep doing a next, we will never reach null. Thus, we can be… Read More »

## Remove duplicates from a sorted linked list.

Question: Given a sorted Linked List. Write a program to remove all the duplicates. Input: 4 -> 8 -> 8 -> 8 -> 15 -> 16 -> 23 -> 23 -> 42. Output: 4 -> 8 -> 15 -> 16 -> 23 -> 42 In a sorted Linked List, all the node that are duplicate will be together.… Read More »

## Write a code to check if 2 linked lists are identical.

Question: Write a program to check if 2 linked lists are identical? Method 1(Iterative):- To Determine if both Linked lists are identical, we need to traverse both lists simultaneously, and while traversing we need to compare data of each node. Implementation Method2( Recursive):- It may seem that both recursive and iterative implementation are the same but the iterative… Read More »

## Implementation of a stack

An implementation of stack is similar to that of a Linked List. We will try to discuss the basic operations in a stack discussed in this post. MAIN OPERATIONS ON A STACK:- push( int ) :- Inserts a data into the stack pop :- Returns the topmost data from the stack AUXILIARY OPERATIONS ON A STACK int Top(… Read More »

## Find the merging point of 2 Linked Lists. Popularly called the Y-node problem.

Question: You are given the head pointer to 2 single Linked Lists, and both of the lists converge at a certain point and then advance forward. We need to find the address of the node, at which both of the Linked Lists meet. Input: 4 -> 8 -> 15 ->                               42 -> 99     16 -> 23… Read More »

## Copy an entire Linked List.

Copying an entire Linked List, means allocating new memory and copying all the data to the new location. This requires traversing each node and copying its data to a new location, keeping the original data intact.

## How will you check if a Linked List is palindrome or not?

Question: Write a program to check if the given Linked List is a palindrome or not? Input: 1.> 4, 8, 15, 16, 23, 42 2.> 4, 8, 15, 16, 15, 8, 4 Output: 1.> IS NOT A PALINDROME 2.> IS A PALINDROME What is a Palindrome? According to Wikipedia, A palindrome is a word, phrase, number, or other… Read More »

## How will you find the middle of a Linked List?

Question: Given a Linked List, we need to find the middle of the list and print the number. Input: 4, 8, 15, 16, 23, 42, 99 Output: 16 This is a very basic question and is a part of a bigger problem. However, there are several approaches to solve the above problem. BRUTE-FORCE APPROACH In this approach, we… Read More »

## Reverse a Linked List in pairs.

Question: Write a program to reverse a Linked List in pairs. That means we should reverse the 1st and 2nd numbers, 3rd and 4th numbers and so on. Input: 4, 8, 15, 16, 23, 42 Output: 8, 4, 16, 15, 42, 23 The basic idea behind solving this problem is that we need to traverse the entire list… Read More »