cci-Q2.1 未排序链表去重
来源:互联网 发布:淘宝古装衣服 编辑:程序博客网 时间:2024/05/22 01:26
原文:
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?
译文:
从一个未排序的链表中移除重复的项
进一步地,
如果不允许使用临时的缓存,你如何解决这个问题?
首先定义一个node类
public class LinkedListNode { int data; LinkedListNode next; public LinkedListNode(int data) { this.data = data; this.next = null; }}
1,允许使用临时的缓存,使用一个哈希表,如果遇到没有遇到过的元素写入哈希表,如果遇到的是重复的,删除。
public static void main(String args[]) { LinkedListNode head = null; LinkedListNode tmp = null; int a[] = {1, 2, 3, 3, 2, 1, 4}; for (int i = 0; i < a.length; i++) { LinkedListNode next = new LinkedListNode(a[i]); if (i == 0) { head = tmp = next; continue; } tmp.next = next; tmp = next; } // Print the list(before) tmp = head; while (tmp != null) { System.out.println(tmp.data); tmp = tmp.next; }; delDup1(head); // Print the list(after) while (head != null) { System.out.println(head.data); head = head.next; }; } public static void delDup1(LinkedListNode head) { LinkedListNode n = head; Hashtable<Integer, Integer> table = new Hashtable<Integer, Integer>(); LinkedListNode previous = null; while (n != null) { if (table.containsKey(new Integer(n.data))) { previous.next = n.next; } else { table.put(new Integer(n.data), new Integer(1)); previous = n; } n = n.next; } }
2,不允许使用临时的缓存
public static void main(String args[]) { LinkedListNode head = null; LinkedListNode tmp = null; int a[] = {1, 2, 3, 3, 2, 1, 4}; for (int i = 0; i < a.length; i++) { LinkedListNode next = new LinkedListNode(a[i]); if (i == 0) { head = tmp = next; continue; } tmp.next = next; tmp = next; } // Print the list(before) tmp = head; while (tmp != null) { System.out.println(tmp.data); tmp = tmp.next; }; delDup(head); // Print the list(after) while (head != null) { System.out.println(head.data); head = head.next; }; } public static void delDup(LinkedListNode head) { if (head == null) { return; } LinkedListNode runner = head; LinkedListNode runnernext = head; LinkedListNode current = head; while (current != null) { runner = current; runnernext = current.next; int currentData = current.data; while (runnernext != null) { if (runnernext.data == currentData) { runner.next = runnernext.next; runnernext = runner.next; } else { runner = runnernext; runnernext = runner.next; } } current = current.next; } }
- cci-Q2.1 未排序链表去重
- cci-Q2.2 链表倒数第n节点值
- cci-Q2.4 链表的值相加
- cci-Q2.5 寻找环形链表的起点
- CCI 2.1 移除未排序链表中的重复节点
- CCI 习题笔记 1
- cci-Q2.3 只给出中间节点,删除链表中间节点
- CCI习题 1-2:Reverse String
- CCI(1)-- Arrays and Strings
- Cracking the coding interview--Q2.1
- Cracking the coding interview--Q2.1
- 【CareerCup】 Linked Lists—Q2.1
- Cracking the Coding Interview Q2.1
- Cracking the coding interview--Q2.1
- CCI习题1-1:String Contain Unique Char
- cci-Q1.1 判断字符串中字符是否唯一
- cci-Q3.1 一个数组实现三个栈
- CCI: my2DAlloc
- 【科研论文】W5100在FPGA系统中实现TCP/IP网络通信
- cocos2d-x 以c++为主 引入LUA 与动态更新
- mysql处理存在则更新,不存在则插入(多列唯一索引)
- Webkit for Android分析
- java synchronized详解
- cci-Q2.1 未排序链表去重
- StringBulider和StringBuffer笔记
- Oracle错误一览表
- GUI系统之SurfaceFlinger(10)SurfaceFlinger的启动与工作原理
- 我本人一直以来犯的错误,在看了《Think In Java》
- java代理模式
- H.264句法和语法总结(一)句法元素的分层结构
- 百度云使用经验
- [- ddms] Can't bind to local 8600 for debugger