题目:删除排序链表中的重复元素
2015-08-19 18:57
399 查看
给定一个排序链表,删除所有重复的元素每个元素只留下一个。
您在真实的面试中是否遇到过这个题?
Yes
哪家公司问你的这个题?
Airbnb
Alibaba
Amazon Apple
Baidu Bloomberg
Cisco Dropbox
Ebay Facebook
Google Hulu
Intel Linkedin
Microsoft NetEase
Nvidia Oracle
Pinterest Snapchat
Tencent Twitter
Uber Xiaomi
Yahoo Yelp
Zenefits
感谢您的反馈
样例
给出1->1->2->null,返回
1->2->null
给出1->1->2->3->3->null,返回 1->2->3->null
标签 Expand
链表
思想:将链表转换成数组,剔除一样的数据,再生成链表
/**
* Definition for ListNode
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
/**
* @param ListNode head is the head of the linked list
* @return: ListNode head of linked list
*/
public static ListNode deleteDuplicates(ListNode head) {
// write your code here
if(head == null) return null;
int count = 0;
ListNode x = head;
while(x!=null){
count++;
x = x.next;
}
int[] listArr = new int[count];
int f = 0;
ListNode y = head;
while(y!=null){
listArr[f++] = y.val;
y = y.next;
}
int k = 0;
for(int i=0;i<listArr.length;i++){
int j = 0;
while(j<k){
if(listArr[i]==listArr[j]){
break;
}
j++;
}
if(j==k){
listArr[k++] = listArr[i];
}
}
int[] resArr = new int[k];
for(int m=0;m<k;m++){
resArr[m] = listArr[m];
}
ListNode reNode = new ListNode(resArr[0]);
ListNode z = reNode;
for(int i=1;i<k;i++){
z.next = new ListNode(resArr[i]);
z = z.next ;
}
z.next = null;
return reNode;
}
}
另一种AC代码
分析:由于有序,所以p结点是否重复只需要和它的前一节点比较是否相等就可以了,我们可以定义一个helper新头结点链表
将p结点与新链表的尾结点比较,若不相等则加入新链表中。
/**
* Definition for ListNode
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
/**
* @param ListNode head is the head of the linked list
* @return: ListNode head of linked list
*/
public static ListNode deleteDuplicates(ListNode head) {
// write your code here
if(head == null) return null;
ListNode res = new ListNode(head.val);
ListNode f = res ;
ListNode x = head.next;
while(x.next!=null){
if(x.val != f.val){
f.next = x;
f = f.next;
}
x=x.next;
}
if(x.val!=f.val){
f.next = x;
}else{
f.next = null;
}
return res;
}
}
给定一个排序链表,删除所有重复的元素每个元素只留下一个。
您在真实的面试中是否遇到过这个题?
Yes
哪家公司问你的这个题?
Airbnb
Alibaba
Amazon Apple
Baidu Bloomberg
Cisco Dropbox
Ebay Facebook
Google Hulu
Intel Linkedin
Microsoft NetEase
Nvidia Oracle
Pinterest Snapchat
Tencent Twitter
Uber Xiaomi
Yahoo Yelp
Zenefits
感谢您的反馈
样例
给出1->1->2->null,返回
1->2->null
给出1->1->2->3->3->null,返回 1->2->3->null
标签 Expand
链表
思想:将链表转换成数组,剔除一样的数据,再生成链表
/**
* Definition for ListNode
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
/**
* @param ListNode head is the head of the linked list
* @return: ListNode head of linked list
*/
public static ListNode deleteDuplicates(ListNode head) {
// write your code here
if(head == null) return null;
int count = 0;
ListNode x = head;
while(x!=null){
count++;
x = x.next;
}
int[] listArr = new int[count];
int f = 0;
ListNode y = head;
while(y!=null){
listArr[f++] = y.val;
y = y.next;
}
int k = 0;
for(int i=0;i<listArr.length;i++){
int j = 0;
while(j<k){
if(listArr[i]==listArr[j]){
break;
}
j++;
}
if(j==k){
listArr[k++] = listArr[i];
}
}
int[] resArr = new int[k];
for(int m=0;m<k;m++){
resArr[m] = listArr[m];
}
ListNode reNode = new ListNode(resArr[0]);
ListNode z = reNode;
for(int i=1;i<k;i++){
z.next = new ListNode(resArr[i]);
z = z.next ;
}
z.next = null;
return reNode;
}
}
另一种AC代码
分析:由于有序,所以p结点是否重复只需要和它的前一节点比较是否相等就可以了,我们可以定义一个helper新头结点链表
将p结点与新链表的尾结点比较,若不相等则加入新链表中。
/**
* Definition for ListNode
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
/**
* @param ListNode head is the head of the linked list
* @return: ListNode head of linked list
*/
public static ListNode deleteDuplicates(ListNode head) {
// write your code here
if(head == null) return null;
ListNode res = new ListNode(head.val);
ListNode f = res ;
ListNode x = head.next;
while(x.next!=null){
if(x.val != f.val){
f.next = x;
f = f.next;
}
x=x.next;
}
if(x.val!=f.val){
f.next = x;
}else{
f.next = null;
}
return res;
}
}
相关文章推荐
- main() 参数:argc 与 argv
- Android笔记(三)为按钮添加事件
- linux下类似Bus Hound的工具
- 字符串常用函数汇总【持续更新中........】
- [LightOJ1370]Bi-shoe and Phi-shoe
- 题目:删除排序数组中的重复数字 II
- Linux 命令行Shell快捷键
- HDU-1875 畅通工程再续(最小生成树[Prim])
- 暗色调,调出紫红色的暗调晨曦照片效果(转)
- ios 点击返回顶部效果的实现,类似单击状态栏效果
- 学习资料备忘录
- 【LeetCode】116. Populating Next Right Pointers in Each Node
- 在 Java 中如何更高效地存储和管理 SQL 语句?
- Oracle 设置表空间自增长
- eclipse 终极操作技巧
- 在 Java 中如何更高效地存储和管理 SQL 语句?
- 浅析关系数据库和NoSql非关系数据库
- 题目:删除排序数组中的重复数字
- Android实例-监测网络状态及一些事件(XE8+小米2)
- 题目:删除元素