How to reverse a linked list in java using iterative

Write a program to reverse a LinkedList in Java using iterative. Solution: 1) Traverse through the linked list 2) Assign as below in loop      next=prev;      prev=current;      current=next;

 Output:

 

How to find/detect infinite loop of linked list in java with example.

There are multiple solutions to solve this. But we will discuss Floyd’s Cycle-Finding Algorithm. This algorithm also called as “Tortoise and Hare Algorithm”. Solution: 1)      Initialize 2 pointers called fast and slow with header element. Move slow pointer by one element and fast pointer by two elements 2)      If these 2 pointers meet at some […]

Find the Nth node/element from a LinkedList in java

You have given a LinkedList (not java.util.LinkedList), and a number N. Write a program to find/print the value at the Nth node from a LinkedList in Java. Solution: 1)      Initialize length to zero 2)      Traverse through the list If length is equal to the passed index, return current element Increment length Assign current element to […]

Find middle node/element of a LinkedList in Java using length of a linked list.

Write a program to find/print middle element of a Linked list in Java using length of a linked list Solution: 1)      Find the length of linked list. 2)      Print the middle of a linked list at (length/2) th node/element.

 Output:

 

Find middle node/element of a LinkedList in Java in one pass.

Write a program to find/print middle element of a Linked list in Java in one pass. Solution: 1)      Initialize 2 pointers called fast and slow with header element. Move slow pointer by one element and fast pointer by two elements 2)      Once the fast pointer reaches end, then slow pointer will reach middle of a […]

Find Nth node/element from the end of a LinkedList in java

You have given a LinkedList (not java.util.LinkedList), and a number N. Write a program to find/print the value at the Nth node from end of a LinkedList in Java. Solution1 (Using length of a LinkedList): 1)      Find the length of linked list. 2)      Print the (length-n+1) th node/element from starting of the linked list.

[…]