Write code to remove duplicates from an unsorted linked list.
FOLLOW UP
How would you solve this problem if a temporary buffer is not allowed?
Solution:
public ListNode removeDuplicate(ListNode head) { ListNode dummy = head; Set<Integer> set = new HashSet<Integer>(); ListNode pre = null; while(head!=null) { if(set.add(head.val)) { pre = head; } else { pre.next = head.next; } head = head.next; } return dummy; }
Followup
public ListNode removeDupsWithoutBuffer(ListNode head) { ListNode cur = head; while(cur!=null) { ListNode n = cur; while(n.next!=null) { if(n.next.val == cur.val) { n.next = n.next.next; } else { n = n.next; } } cur = cur.next; } return head; }
相关推荐
LeetCode Remove Duplicates from Sorted Array解决方案
26.Remove_Duplicates_from_Sorted_Array删除有序数组中的重复项【LeetCode单题讲解系列
RemoveDuplicates for version 7.1
获取从index=1及之后的数据 ${fromlist} Get Slice From List ${list} 1 # 获取从index=1~2的数据,不包括第3个 ${fromtolist} Get Slice From List ${list} 1 3 4 Remove Duplicates-去重 ⽰例: @{list} Create ...
demonstrates the algorithm to remove duplicates in an array of integers using vectors and unique.
题目描述(中等难度) 给一个链表,如果一个数属于重复数字,就把这个数删除,一个都不留。 解法一 迭代 只需要两个指针,一个指针 pre 代表重复数字的前边的一个指针,另一个指针 cur 用来遍历链表。...
Remove_duplicates.cpp
键值对文档去重,键重复部分挑出,以及值为英文时首字母大写。如果想要排序可以先把内容放到excel中排序,中间不能有多余的回车,结尾回车一个。
Duplicates from Sorted List 141 Easy Linked List Cycle 160 Easy Intersection of Two Linked Lists 203 Easy Remove Linked List Elements no 206 Easy Reverse Linked List 234 Easy Palindrome Linked List
Remove Duplicates from Sorted List II Rotate List Remove Nth Node From End of List Swap Nodes in Pairs Reverse Nodes in k-Group Copy List with Random Pointer Linked List Cycle Linked List Cycle II ...
删除重复项 从数组中删除重复条目的功能 使用npm install安装依赖项 使用gulp test运行gulp test
Unsorted Array Easy 27 Remove Element Easy 剑指Offer 考点 题目 解法 难度 字符串 替换空格 易 字符串 字符串旋转 易 数组 二维数组中的查找 难 数组 旋转数组的最小数字 难 数组 打印1到最大的n位数 难 栈和队列...
Duplicates from Sorted List Palindrome Linked List LL中的插入排序 使用额外的缓冲区从未排序的链表中删除重复项 细绳 确定字符串是否包含所有唯一字符 (CTCI) 在不使用额外缓冲区的情况下删除字符串中的重复字符...
Remove Duplicates from Sorted Array 2 Remove Duplicates from Sorted Array II 3 Search in Rotated Sorted Array 4 Search in Rotated Sorted Array II 5 Median of Two Sorted Arrays 递归实现find kth 6 ...
26.Remove Duplicates from Sorted Array 53.Maximum Subarray 70.Climbing Stairs 121.Best Time to Buy and Sell Stock 122.Best Time to Buy and Sell Stock II 123.Best Time to Buy and Sell Stock III 141....
leetcode 2 sum c LeetCode 贵有恒,何必三更起五更睡;最无益,只怕一日暴十寒。 我的个人网站: 分享技术,乐享生活:Jack ...Duplicates from Sorted List 141 * Linked List Cycle 160 * Intersection of Two Linke
leetcode 2 sum c LeetCode 贵有恒,何必三更起五更睡;最无益,只怕一日暴十寒。 我的个人网站: 分享技术,乐享生活:Jack ...Duplicates from Sorted List 141 * Linked List Cycle 160 * Intersection of Two Linke
Duplicates from Sorted Array Easy #27 Remove Element Easy #35 Search Insert Position Easy #38 Count and Say Easy #53 Maximum Subarray Easy #66 Plus One Easy #70 Climbing Stairs Easy #83 Remove ...
remove-duplicates-from-sorted-list ii 83 删除排序链表中的重复元素 remove-duplicates-from-sorted-list 86 分隔链表 partition-list 92 反转链表 II reverse-linked-list-ii(Reverse a Sub-list) 141 环形链表...
II(Remove Duplicates from Sorted List II) 2018.9.27 重建二叉树(Rebuild Binary Tree) 2018.9.28 把字符串转换成整数(Convert a string to an integer) 2018.10.8 树的子结构(Substructure of the tree) ...