Home › Company-wise questions › PayPal › Remove Duplicates from Sorted List II ← PayPal bank
PayPal · Question 38 · Linked Lists
Remove Duplicates from Sorted List II Linked Lists Java code ⏱ O(n) 💾 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 Remove all nodes that have duplicate numbers, leaving only distinct numbers.
Approach Use dummy node and skip duplicates completely.
Time O(n)Space O(1)
How it works 1 Jump whole duplicate runs. 2 Singletons link through prev. Sample input / output Input:
head = [1, 2, 3, 3, 4, 4, 5]
Output:
[1, 2, 5] Constraints 0 <= nodes <= 300; sorted list.
Solution — read it, then walk it Flowchart Pseudocode
Time O(n)Space O(1)Loops 4Branches 1Returns 3source · Java
Scroll sideways for wide charts — the view follows the active step. ⤢ Expand
yes no ↻ repeat ↻ 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 deleteDuplicatesII(ListNode head) ListNode dummy = new ListNode(0) dummy.next = head ListNode prev = dummy while (head != null) if (head.next != null && head.val == head.next.val) while (head.next != null && head.val == head.next.val) head = head.next prev.next = head.next } else head = head.next RETURN dummy.next DEFINE static void main(String[] args) System.out.println(toList(deleteDuplicatesII(buildList(new int[]{1, 2, 3, 3, 4, 4, 5})))) END start / end statement condition loop returns definition← Q37 · Remove Duplicates from Sorted List Q39 · Intersection of Two Linked Lists → 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 →