数据结构—链表的操作小结
来源:互联网 发布:哪个炒股软件最好 编辑:程序博客网 时间:2024/05/29 19:27
总结一下链表的相关操作
1判断是否有环
2找到环的交界
3返回环的长度
4两个链表的公共节点
5链表逆置
6带有random指针的链表复制
import java.util.HashMap;import java.util.Map;/** * Created by root on 17-11-9. */class Node{ int val; Node next;}class Node3{ int val; Node3 next; Node3 random;}public class 链表 { public static void main(String args[]){ } //判断是否是环--一个快指针,一个慢指针,快指针是慢指针速度的两倍,指针若能相遇,则存在环 public boolean isCircle(Node head){ Node slow = head; Node fast = head; while (fast!=null&&fast.next!=null){ slow=slow.next; fast=fast.next.next; if(slow==fast){ return true; } } return false; } //找到环的起始位置--相遇后,slow指针放到头,slow和fast一起移动到相遇 public Node FindCircle(Node head){ Node slow = head; Node fast = head; while (fast!=null&&fast.next!=null){ slow=slow.next; fast=fast.next.next; if (slow==fast){ break; } } if (fast==null||fast.next==null){ return null; } slow=head; while (slow!=fast){ slow=slow.next; fast=fast.next; } return slow; } //返回环的长度--相遇以后,一个不动,一个一步一步的移动到相遇,这段长度就是 public int CircleLength(Node head){ Node slow = head; Node fast = head; while (fast!=null&&fast.next!=null){ slow=slow.next; fast=fast.next.next; if (slow==fast){ break; } } if (fast==null||fast.next==null){ return 0; } fast=fast.next; int count=1; while (fast!=slow){ fast=fast.next; count++; } return count; } //找到两个节点的公共节点,先进行长度作差,长的先移动,然后同时移动进行判断是否重叠 public Node CommonNode(Node node1,Node node2){ if (node1==null||node2==null)return null; Node head1=node1; Node head2=node2; int count1=0,count2=0; while (head1!=null){ head1=head1.next; count1++; } while (head2!=null){ head2=head2.next; count2++; } int sub =Math.abs(count1-count2); head1=node1; head2=node2; if(count2>count1){ while (sub--!=0){ head2=head2.next; } }else{
while (sub--!=0){ head1=head1.next; } }
while (head1!=null&&head2!=null){ if (head1==head2)return head1; else { head1=head1.next; head2=head2.next; } } return null; } //逆置链表,头插法 Node ReverseList(Node head){ Node current = head.next; Node next = current.next; head.next=null; while (current!=null){ current.next=head.next; head.next=current; current=next; next=current.next; } return head; } //复制带有random指针的链表,先构建hashmap,然后再执行操作 Node3 copyList(Node3 head){ Node3 l2,l1=head; Map<Node3,Node3> ht = new HashMap<Node3,Node3>(); while (l1!=null){ l2=new Node3(); l2.val=l1.val; ht.put(l1,l2); l1=l1.next; } l1=head; while (l1!=null){ l2=ht.get(l1); l2.next=ht.get(l1.next); l2.random=ht.get(l1.random); l1=l1.next; } return ht.get(head); }}
阅读全文
0 0
- 数据结构—链表的操作小结
- 数据结构—基本树的操作小结
- 数据结构——链表的操作
- 数据结构链表的操作
- 数据结构中线性表和链表的小结
- 数据结构——双向链表的有关操作
- 数据结构——链表的基本操作
- 数据结构—(1)链表的基本操作
- 数据结构 链表的基本操作
- 数据结构:链表的简单操作
- 数据结构:多项式链表的操作
- 数据结构链表的一些操作!
- 数据结构:链表的基本操作
- 数据结构--二叉链表的基本操作
- 【数据结构】链表的基本操作
- 数据结构与算法 链表的操作
- 数据结构之链表的基本操作
- C++数据结构 链表的基本操作
- maven deploy到nexus报错:Return code is: 401, ReasonPhrase:Unauthorized
- iframe session失效,跳转登录页面
- 使用ajax跨域请求时,后台无法获取到token
- 恢复git误操作删除的远程分支
- Nodejs中Log4js使用
- 数据结构—链表的操作小结
- 数组与字符串间转换
- xlistview上拉加载 下拉刷新
- Irrelevant Elements UVA
- python+selenium自动化测试之路Day5之浏览器的基本操作方法详解
- Java并发编程线程中:Callable、Future和FutureTask类
- Codeforces Round #444 (Div. 2) C
- jQuery中ajax学习记录
- jsonp原理详解