Postingan

Menampilkan postingan dengan label linked list

How to Find Middle Element of Linked List in Java in Single Pass

Gambar
ow do you find the middle element of LinkedList in one pass is a programming question often asked Java and non-Java programmers in telephonic Interview. This question is similar to checking palindrome or calculating the factorial, where Interviewer sometimes also ask to write code. In order to answer this question candidate must be familiar with LinkedList data structure i.e. In the case of singly LinkedList, each node of Linked List contains data and pointer, which is the address of next Linked List and the last element of Singly Linked List points towards the null. Since in order to find middle element of Linked List you need to find the length of linked list, which is counting elements till end i.e. until you find the last element of Linked List. What makes this data structure Interview question interesting is that you need to find the middle element of LinkedList in one pass and you don’t know the length of LinkedList. This is where candidates logical ability puts into the test...

How to find the 3rd (kth) Node from end or tail in a linked list in Java

Gambar
Hello guys, the problem to find the 3rd element from the end in a singly linked list or Kth node from the tail is one of the tricky but frequently asked linked list problems in Programming job interviews. I know you can easily solve this problem by moving from tail to head or in the reverse direction but the main challenge here is to solve the problem in just one pass. That means, you can not traverse the linked list again and you cannot traverse backward because it's a singly linked list. So what do you think? Isn't this problem challenging? Well, I did struggle when I saw this problem very first time but once you understand the logic and some tricks to solve a linked list based problem like Recursion then it would be easy for you, and that's what you will learn in this article. If you have practiced some linked list problems likefinding the length, inserting elements, or deleting elements then you should be familiar with how to traverse through a linked list. Here, we...

How do you find length of a Singly Linked list using Loop and Recursion

Gambar
Hello guys, here is one of the classical programming questions how do you find the length of a linked list using recursion and without recursion. This is not about the LinkedList class of Java API but the linked list data structure, which is made of nodes that contain both data and address of the next node. In order to calculate the length, you need to count all nodes in the linked list. Since it's a singly linked list you can only move in one direction from the head to tail.  This coding problem was asked to me on my first interview with a multinational Investment bankAfter that, this question has been asked to me on several occasions in other Programming Job Interviews as well. What makes this question interesting is that Java developers are not that great with the data structure as compared to a C++ developer who is evident because of the fundamental difference between these two languages. C++ is more of a system programming language, while Java is more on application programmin...