**Linked List Intersection - Amazon Top Interview Questions**

### Problem Statement :

Given two sorted linked lists l0, and l1, return a new sorted linked list containing the intersection of the two lists. Constraints n ≤ 100,000 where n is the number of nodes in l0 m ≤ 100,000 where m is the number of nodes in l1 Example 1 Input l0 = [1, 3, 7] l1 = [2, 3, 7, 9] Output [3, 7] Example 2 Input l0 = [1, 2, 3] l1 = [4, 5, 6] Output None

### Solution :

` ````
Solution in C++ :
LLNode* solve(LLNode* l0, LLNode* l1) {
if (l0 == NULL or l1 == NULL) return NULL;
LLNode *curr = NULL, *head = NULL;
LLNode* ptr1 = l0;
LLNode* ptr2 = l1;
while (ptr2 and ptr1) {
if (ptr2->val == ptr1->val) {
LLNode* newnode = new LLNode(ptr1->val);
if (head == NULL) {
curr = newnode;
head = newnode;
} else {
curr->next = newnode;
curr = curr->next;
}
ptr1 = ptr1->next;
ptr2 = ptr2->next;
}
else {
if (ptr1->val > ptr2->val)
ptr2 = ptr2->next;
else
ptr1 = ptr1->next;
}
}
return head;
}
```

` ````
Solution in Java :
import java.util.*;
class Solution {
public LLNode solve(LLNode l0, LLNode l1) {
LLNode head = new LLNode();
LLNode tail = head;
while (l0 != null && l1 != null) {
if (l0.val == l1.val) {
tail.next = l0;
tail = tail.next;
l0 = l0.next;
l1 = l1.next;
} else if (l0.val > l1.val)
l1 = l1.next;
else
l0 = l0.next;
}
tail.next = null;
return head.next;
}
}
```

` ````
Solution in Python :
class Solution:
def solve(self, l0, l1):
# Remember the lists are sorted
# Also remember that intersection of two linked lists does not mean the lists actually intersect
# like the case where l0 : 3--4---
# |
# l1 1--2--5--6--7
# like l1 is not a part of l0 or vice versa
# This is just plain intersection where we have to find elements that are common in both.
head, tail = None, None
while l0 is not None and l1 is not None:
if l0.val == l1.val:
if head is None:
head = LLNode(l0.val)
tail = head
else:
tail.next = LLNode(l0.val)
tail = tail.next
l0, l1 = l0.next, l1.next
elif l0.val < l1.val:
l0 = l0.next
elif l1.val < l0.val:
l1 = l1.next
return head
```

## View More Similar Problems

## Simple Text Editor

In this challenge, you must implement a simple text editor. Initially, your editor contains an empty string, S. You must perform Q operations of the following 4 types: 1. append(W) - Append W string to the end of S. 2 . delete( k ) - Delete the last k characters of S. 3 .print( k ) - Print the kth character of S. 4 . undo( ) - Undo the last (not previously undone) operation of type 1 or 2,

View Solution →## Poisonous Plants

There are a number of plants in a garden. Each of the plants has been treated with some amount of pesticide. After each day, if any plant has more pesticide than the plant on its left, being weaker than the left one, it dies. You are given the initial values of the pesticide in each of the plants. Determine the number of days after which no plant dies, i.e. the time after which there is no plan

View Solution →## AND xor OR

Given an array of distinct elements. Let and be the smallest and the next smallest element in the interval where . . where , are the bitwise operators , and respectively. Your task is to find the maximum possible value of . Input Format First line contains integer N. Second line contains N integers, representing elements of the array A[] . Output Format Print the value

View Solution →## Waiter

You are a waiter at a party. There is a pile of numbered plates. Create an empty answers array. At each iteration, i, remove each plate from the top of the stack in order. Determine if the number on the plate is evenly divisible ith the prime number. If it is, stack it in pile Bi. Otherwise, stack it in stack Ai. Store the values Bi in from top to bottom in answers. In the next iteration, do the

View Solution →## Queue using Two Stacks

A queue is an abstract data type that maintains the order in which elements were added to it, allowing the oldest elements to be removed from the front and new elements to be added to the rear. This is called a First-In-First-Out (FIFO) data structure because the first element added to the queue (i.e., the one that has been waiting the longest) is always the first one to be removed. A basic que

View Solution →## Castle on the Grid

You are given a square grid with some cells open (.) and some blocked (X). Your playing piece can move along any row or column until it reaches the edge of the grid or a blocked cell. Given a grid, a start and a goal, determine the minmum number of moves to get to the goal. Function Description Complete the minimumMoves function in the editor. minimumMoves has the following parameter(s):

View Solution →