Posts

Showing posts with the label DSA

Data Structures(DSA)

  1. Is it possible to find a loop in a Linked list ? a. Possible at O(n)  b. Not possible  c. Possible at O(n^2) only  d. Depends on the position of loop Solution: (a) Possible at O(n)  Have two pointers say P1 and P2 pointing to the first node of the list.  Start a loop and Increment P1 once and P2 twice in each iteration. At any point of time if P1==P2 then there is a loop in that linked list. If P2 reaches NULL (end of linked list) then no loop exists.   2. Two linked lists L1 and L2 intersects at a particular node N1 and from there all other nodes till the end are common. The length of the lists are not same. What are the possibilities to find N1?. a. Solution exist for certain cases only  b. No linear solution exist  c. Linear solution is possible  d Only Non-linear solution exist. Solution: c. Linear solution is possible Have two pointers say P1 pointing to the first node of L1 and P2 to that of L2. Traverse thr...