Home › Company-wise questions › Uber › Remove duplicates from sorted linked list ← Uber bank
Uber · Question 16 · Linked Lists
Remove duplicates from sorted linked list Linked Lists Python 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 Delete all duplicates in a sorted linked list.
Approach Skip nodes with same value as current.
Time O(n)Space O(1)
How it works 1 Walk with current through the sorted list. 2 Skip next whenever it duplicates current's value. 3 Advance only on distinct values. Sample input / output Input:
5
1 1 2 3 3
Output:
[1, 2, 3] Constraints 0 <= nodes <= 300; -100 <= Node.val <= 100; list is sorted.
Solution — read it, then walk it Flowchart Pseudocode
Time O(n)Space O(1)Loops 3Branches 3Returns 3source · Python
Scroll sideways for wide charts — the view follows the active step. ⤢ Expand
yes no ↻ repeat ↻ repeat ↻ repeat yes no START head current = head while current and current.next if current.val == current.next.val current.next = current.next.next ELSE current = current.next RETURN head CLASS ListNode DEFINE __init__(self, val=0, next=None) self.val = val self.next = next DEFINE build_list(values) dummy = tail = ListNode() for v in values tail.next = ListNode(v) tail = tail.next RETURN dummy.next DEFINE to_list(head) out = [] while head out.append(head.val) head = head.next RETURN out DEFINE main() import sys data = sys.stdin.read().split() it = iter(data) n = int(next(it)) values = [int(next(it)) for _ in range(n)] print(to_list(deleteDuplicates(build_list(values)))) if __name__ == "__main__" main() END start / end statement condition loop returns definition← Q15 · Remove nth node from end of list Q17 · Add two numbers represented by linked lists → Solution code Core logic highlighted in green
Language Pythonsource Java 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 →