careercup-链表 2.1
2.1 编写代码,移除未排序链表中的重复节点。
不使用临时缓存:
如果不允许使用临时的缓存(即不能使用额外的存储空间),那需要两个指针, 当第一个指针指向某个元素时,第二个指针把该元素后面与它相同的元素删除, 时间复杂度O(n2 )。
C++实现代码:
#include<iostream>
#include<new>
using namespace std; struct ListNode
{
int val;
ListNode *next;
ListNode(int x):val(x),next(NULL) {}
}; void createList(ListNode *&L)
{
int arr[]= {,,,,,,,,,};
int i;
ListNode *p=NULL;
for(i=; i<; i++)
{
ListNode *tmp=new ListNode(arr[i]);
if(L==NULL)
{
L=tmp;
p=tmp;
}
else
{
p->next=tmp;
p=tmp;
}
}
} void deleteDup(ListNode *L)
{
if(L==NULL)
return;
ListNode *p=L;
ListNode *q=NULL;
while(p)
{
q=p;
while(q->next)
{
if(q->next->val==p->val)
q->next=q->next->next;
else
q=q->next;
}
p=p->next;
}
} int main()
{
ListNode *head=NULL;
createList(head);
ListNode *p=head;
while(p)
{
cout<<p->val<<" ";
p=p->next;
}
cout<<endl;
deleteDup(head);
p=head;
while(p)
{
cout<<p->val<<" ";
p=p->next;
}
cout<<endl;
}
运行结果:

careercup-链表 2.1的更多相关文章
- [CareerCup] 2.1 Remove Duplicates from Unsorted List 移除无序链表中的重复项
		
2.1 Write code to remove duplicates from an unsorted linked list.FOLLOW UPHow would you solve this p ...
 - [CareerCup] 2.2 Kth to Last Element of Linked List 链表的倒数第k个元素
		
2.2 Implement an algorithm to find the kth to last element of a singly linked list. 这道题让我们求链表中倒数第k个元 ...
 - [CareerCup] 2.3 Delete Node in a Linked List 删除链表的节点
		
2.3 Implement an algorithm to delete a node in the middle of a singly linked list, given only access ...
 - [CareerCup] 2.4 Partition List 划分链表
		
2.4 Write code to partition a linked list around a value x, such that all nodes less than x come bef ...
 - [CareerCup] 2.6 Linked List Cycle 单链表中的环
		
2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of ...
 - [CareerCup] 2.7 Palindrome Linked List 回文链表
		
2.7 Implement a function to check if a linked list is a palindrome. LeetCode上的原题,参见我之前的博客Palindrome ...
 - [CareerCup] 4.4 Create List at Each Depth of Binary Tree 二叉树的各层创建链表
		
4.4 Given a binary tree, design an algorithm which creates a linked list of all the nodes at each de ...
 - [CareerCup] 17.13 BiNode 双向节点
		
17.13 Consider a simple node-like data structure called BiNode, which has pointers to two other node ...
 - 二叉树系列 - 二叉搜索树 - 线性时间内把有序链表转化为BST
		
引言 本文来自于Google的一道题目: how to merge two binary search tree into balanced binary search tree. how to me ...
 - Careercup - Google面试题 - 5735304249999360
		
2014-05-03 23:18 题目链接 原题: Insert a element in a sorted circular linked list 题目:题意简单明了,向一个有序的循环单向链表中插 ...
 
随机推荐
- JVM 学习笔记(一)
			
JVM ----Java Virtual Machine (熟称:JAVA虚拟机),JVM 在执行JAVA程序的过程中将内容划分为若干个区域,其有各自的用途和管理机制.如下图: 1. 程序计 ...
 - 在mesos上安装storm,并且执行wordcount测试程序
			
mesos上安装storm 1.基本说明 如果只用一句话来描述storm的话,可能会是这样:分布式实时计算系统.按照storm作者的说法,storm对于实时计算的意义类似于hadoop对于批处理的意义 ...
 - iOS各种系统通知Name
			
当了一次搬运工,原地址:http://blog.csdn.net/db905517804/article/details/50569949 用法: - (void) viewWillAppear:(B ...
 - WEB 开发异常:java.lang.ClassNotFoundException
			
某个类明明是有的,可是eclipse 启动tomcat服务器运行web项目,出现如题异常. java.lang.ClassNotFoundException 信息: Set web app root ...
 - JSON数据与JavaScript对象转换
			
使用JSON时,最基本的工作就是JSON数据与JavaScript对象之间的互相转换.如浏览器 从服务器端获得JSON数据,然后转换为JavaScript对象在网页输出. SON: JavaScrip ...
 - WebView  和Animation冲突
			
当有WebView时,可能会出现 动画没有执行过程. 解决方法: child.setLayerType(View.LAYER_TYPE_HARDWARE, null); child.setAnimat ...
 - Linux的线程同步对象:互斥量Mutex,读写锁,条件变量
			
进程是Linux资源分配的对象,Linux会为进程分配虚拟内存(4G)和文件句柄等 资源,是一个静态的概念.线程是CPU调度的对象,是一个动态的概念.一个进程之中至少包含有一个或者多个线程.这 ...
 - web开发小节.txt
			
我最近一直在看这个的java web项目涉及到的知识,今天突然感觉思路烁然开明. 我们经常会将java web开发说成是MVCV: view 我在这里姑且就介绍JSP吧.JSP其实就是在原来的静态页面 ...
 - 畅通工程 HDOJ--1863
			
畅通工程 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
 - 【转】.NET 三层架构 中 DAL+IDAL+Model+BLL+Web
			
其实三层架构是一个程序最基本的 在.Net开发中通常是多层开发 比如说 BLL 就是business Logic laywer(业务逻辑层) 他只负责向数据提供者也就是DAL调用数据 然后传递给 客户 ...