Home › Company-wise questions › Google › Remove Duplicates from Sorted List ← Google bank
Google · Question 32 · Linked Lists
Remove Duplicates from Sorted 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 duplicates from a sorted linked list such that each element appears once.
Approach Iterate and remove nodes with duplicate values.
Time O(n)Space O(1)
How it works 1 Traverse the list with pointer current. 2 If current and next values match, skip the next node. 3 Otherwise, move to the next node. 4 Return the cleaned list. Sample input / output Input:
3
1 1 2
Output:
[1, 2] 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← Q31 · Copy List with Random Pointer Q43 · Implement Queue using Stacks → 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 →