Home › Company-wise questions › PayPal › Intersection of Two Linked Lists ← PayPal bank
PayPal · Question 39 · Linked Lists
Intersection of Two Linked Lists Linked Lists Java code ⏱ O(n + m) 💾 O(1)
Product-based Published and maintained by DSA-topics · Last updated 30 November 2026 · Report an error
About this bank These are classic technical-interview concepts compiled from publicly circulated company-wise question sets, rewritten with original explanations and compiler-ready solutions for educational practice; inclusion does not imply endorsement by the company. Spotted an error? Tell us .
Problem Find the node at which two singly linked lists intersect or null if no intersection.
Approach Use two pointers that switch lists once they reach the end.
Time O(n + m)Space O(1)
How it works 1 Each pointer switches lists once. 2 Equal total distance aligns them. Sample input / output Input:
headA = [4, 1, 8, 4, 5], headB = [5, 6, 1, 8, 4, 5]
Output:
8 Constraints 1 <= nodes <= 3 * 10^4; O(1) memory.
Solution — read it, then walk it Flowchart Pseudocode
Time O(n + m)Space O(1)Loops 4Branches 1Returns 4source · Java
Scroll sideways for wide charts — the view follows the active step. ⤢ Expand
↻ repeat START import java.util.* DEFINE class Main DEFINE class ListNode DEFINE val ListNode next ListNode(int v) { val = v; } DEFINE ListNode buildList(int[] values) ListNode dummy = new ListNode(0), tail = dummy for (int v : values) { tail.next = new ListNode(v); tail … RETURN dummy.next DEFINE String toList(ListNode head) StringBuilder sb = new StringBuilder("[") while (head != null) { sb.append(head.val); head = head.next; i… RETURN sb.append("]").toString() DEFINE static ListNode getIntersectionNode(ListNode headA, ListNode headB) if (headA == null || headB == null) return null ListNode a = headA, b = headB while (a != b) a = (a == null) ? headB : a.next b = (b == null) ? headA : b.next RETURN a DEFINE ListNode tailOf(ListNode h) while (h.next != null) h = h.next RETURN h DEFINE static void main(String[] args) ListNode shared = buildList(new int[]{8, 4, 5}) ListNode a = buildList(new int[]{4, 1}) tailOf(a).next = shared ListNode b = buildList(new int[]{5, 6, 1}) tailOf(b).next = shared ListNode node = getIntersectionNode(a, b) System.out.println(node == null ? "null" : node.val) END start / end statement condition loop returns definition← Q38 · Remove Duplicates from Sorted List II Q40 · Reverse Nodes in k-Group → Solution code Core logic highlighted in green
Language Python Javasource C++ C Core logic Full code Copy
Recommended resources Affiliate links · may earn us a commission
Classic DSA interview books The reference books interviewers themselves learned from — great next to the visualizers. See the books → Structured coding courses Video courses that pair well with visual practice — pick one language and go deep. Browse courses → Cloud hosting for side projects Ship the portfolio project that gets you hired — free credits for new accounts. Claim credits →