删除无序链表中值重复出现的节点
来源:互联网 发布:淘宝设计美工招聘 编辑:程序博客网 时间:2024/06/08 17:46
import java.util.HashSet;//删除无序链表中值重复出现的节点public class DelSameList{ //节点的定义 public static class Node{ int value; Node next; public Node(int data) { this.value=data; } } //(一、哈希表法 时间复杂度O(n),空间复杂度O(n))删除无序链表中值重复出现的节点 public static void delSameList(Node head) { if(head==null) { return ; } HashSet<Integer> set=new HashSet<Integer>(); //存储不相同节点的值 Node p=head; Node cur=head.next; set.add(head.value); //首先加入头节点 while(cur!=null) { if(set.contains(cur.value)) { p.next=cur.next; //删除cur这个节点 }else{ set.add(cur.value); p=cur; } cur=cur.next; } } //(一、选择排序法 时间复杂度O(n*n),空间复杂度O(1))删除无序链表中值重复出现的节点 public static void delSameList2(Node head) { if(head==null) { return; } Node cur=head; Node pre=null; Node next=null; while(cur!=null) { pre=cur; next=cur.next; while(next!=null) { if(cur.value==next.value) { pre.next=next.next; //删除next节点 } else { pre=next; //pre向下移动一个节点 } next=next.next; } cur=cur.next; } } //打印链表 public static void PrintList(Node head) { while(head!=null) { System.out.print(head.value+" "); head=head.next; } System.out.println(); }public static void main(String[]args){ Node node=new Node(1);node.next=new Node(2);node.next.next=new Node(3);node.next.next.next=new Node(3);node.next.next.next.next=new Node(4);node.next.next.next.next.next=new Node(4);node.next.next.next.next.next.next=new Node(2);node.next.next.next.next.next.next.next=new Node(1);node.next.next.next.next.next.next.next.next=new Node(1); PrintList(node);delSameList2(node); //删除重复的节点 PrintList(node);}}
阅读全文
0 0
- 删除无序链表中值重复出现的节点
- 链表问题---删除无序单链表中值重复出现的节点
- 算法优解(8)-删除无序单链表中重复出现的节点
- 删除链表中值相同的多余节点
- java 删除链表中值为x的节点
- 删除链表中值为value的节点
- 删除无序链表中的重复值
- 删除排序链表的重复节点
- 删除链表重复节点
- 【IT笔试面试题整理】删除无序链表中重复的节点
- CareerCup之2.1无序链表删除重复元素
- C++删除单链表中值重复的结点
- 删除排序链表的的重复节点
- 删除排序链表的所有重复节点
- 删除链表中的重复节点
- 删除链表中的重复节点
- 删除有序链表中的重复节点
- 删除有序链表中的重复节点
- #include <string.h> 、<cstring>、<string> 的区别
- Java JDK动态代理实现自己的事务管理器
- pthread_cond_wait执行失败
- Linux下后台执行的程序出现stopped的情况
- 初学Qt:链表迭代器
- 删除无序链表中值重复出现的节点
- 网狐荣耀版棋牌游戏输赢控制设置
- 微信清理内置浏览器缓存
- Android Camera对预览进行黑白滤镜处理
- iOS上线提审问题
- MongoDB查询
- 设计模式六大原则
- 关于在cocos studio设置旋转度的bug
- 使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)