数据结构之链表及相关算法
来源:互联网 发布:艾克里里淘宝店铺号 编辑:程序博客网 时间:2024/05/21 22:45
链表的定义
链表是链式存储结构,可以用任意一组存储单元来存储单链表中的数据元素,存储单元可以是不连续的。除了存储每个数据元素的值之外,还必须存储知识其直接后继元素的信息。定义如下的数据类存储结点信息。
//链表结点的定义class Node{ Node next = null; // 结点域 int data; // 数据域 public Node(int data){ this.data = data; }}
链表的创建
给定一个数组,用链表来存储这个数组。循环遍历数组,把每个元素链接到链表中,最后返回头结点。
public class TestNode{ // 输入一个整型数组,创建成链表 public Node createLinkList(int[] arr){ Node head = new Node(arr[0]); //链表头 Node r = head; // 指向链表尾结点 for(int i=1; i<arr.length; i++){ Node pNode = new Node(arr[i]); // 新结点的产生 r.next = pNode; // 链表尾部增加新结点 r = pNode; // r更新为新加的结点 } return head; } // 返回链表的长度 public int length(Node head) { int length = 0; Node tmp = head; while(tmp != null){ length++; tmp = tmp.next; } return length; } // 顺序打印链表元素信息 public void printLinkList(Node head){ Node p = head; while(p != null){ System.out.print(p.data+"->"); p = p.next; } System.out.println(); } public static void main(String[] args) { int[] arr = {1,3,6,8,12,15,20}; TestNode testNode = new TestNode(); Node head = testNode.createLinkList(arr); testNode.printLinkList(head); }}
链表的相关算法
1. 找出单链表中的倒数第k个元素
设置两个指针p1、p2,p1先前移k-1步,然后p1、p2同时向前移动。循环直到先行的p1值为NULL时,p2所指的位置就是所要找的倒数第k个位置。
//找出单链表中的倒数第k个元素public Node findElem(Node head, int k){ if(k < 1 || k > this.length(head)) return null; Node p1 = head; Node p2 = head; for(int i=0; i<k-1; i++) // p1先走k-1步 p1 = p1.next; while(p1 != null){ // p1,p2同时走,直到p1为null p1 = p1.next; p2 = p2.next; } return p2;}
2. 实现链表的反转
反转一个链表,需要调整指针的指向,具体需要操作3个相邻的结点。
//如何实现链表的反转public Node ReverseIteratively(Node head){ Node pReverseHead = head; Node pNode = head; Node pPrev = null; while(pNode != null){ Node pNext = pNode.next; if(pNext == null) pReverseHead = pNode; pNode.next = pPrev; pPrev = pNode; pNode = pNext; } return pReverseHead;}
3. 寻找单链表的中间结点
定义两个指针p、q,从头开始遍历,p一次走两步,q一次走一步,p先到链表尾部,q则恰好到达链表中部。
// 寻找单链表的中间结点public Node SearchMid(Node head){ Node p = head; Node q = head; // 定义两个指针 while(p != null && p.next != null && p.next.next != null){ p = p.next.next; // 一个每次走两步 q = q.next; // 一个每次走一步 } // 每次走两步的指针p到结尾时,每次走一步的指针q刚好到中间结点 return q;}
4. 合并两个有序链表,并使合并后的链表也有序
// 合并两个有序链表,并使合并后的链表也有序public Node mergeLink(Node head1, Node head2){ Node head; Node p1 = head1; Node p2 = head2; if(head1.data < head2.data){ head = new Node(head1.data); p1 = head1.next; }else{ head = new Node(head2.data); p2 = head2.next; } Node pNode = head; while(p1 != null && p2 != null){ if(p1.data > p2.data){ pNode.next = p2; p2 = p2.next; }else{ pNode.next = p1; p1 = p1.next; } pNode = pNode.next; } if(p1 != null) pNode.next = p1; if(p2 != null) pNode.next = p2; return head;}
// 合并两个有序链表的递归方法public Node mergeLink2(Node head1, Node head2){ if(head1 == null) // 检测head1是否为空 return head2; else if(head2 == null) // 检测head2是否为空 return head1; Node head = null; if(head1.data < head2.data){ head = head1; head.next = mergeLink2(head1.next, head2); // 递归调用 }else{ head = head2; head.next = mergeLink2(head1, head2.next); // 递归调用 } return head;}
5. 判断两个链表是否相交
如果两个链表相交,那么一定有相同的尾结点。所以只要判断尾结点是否相等。
public boolean isIntersect(Node h1, Node h2){ if(h1 == null || h2 == null) return false; Node tail1 = h1; while(tail1.next != null) // 找到链表h1的最后一个节点 tail1 = tail1.next; Node tail2 = h2; while(tail2.next != null) // 找到链表h2的最后一个节点 tail2 = tail2.next; return tail1 == tail2;}
6. 找到两个链表相交的第一个节点
// 找到两个链表相交的第一个节点public static Node getFirstMeetNode(Node h1, Node h2){ if(h1 == null || h2 == null) return null; Node tail1 = h1; int len1 = 1; while(tail1.next != null){ // 找到链表h1的最后一个结点 tail1 = tail1.next; len1++; } Node tail2 = h2; int len2 = 1; while(tail2.next != null){ // 找到链表h2的最后一个结点 tail2 = tail2.next; len2++; } if(tail1 != tail2) // 两链表不相交 return null; Node t1 = h1; Node t2 = h2; if(len1 > len2){ // 找出较长的链表先遍历 int d = len1 - len2; while(d != 0){ t1 = t1.next; d--; } }else{ int d = len2 - len1; while(d != 0){ t2 = t2.next; d--; } } while(t1 != t2){ t1 = t1.next; t2 = t2.next; } return t1;}
7. 检测一个链表是否有环
两个指针slow、fast一个一次走一步,一个一次走两步,如果有环,它们一定会相遇。
public boolean IsLoop(Node head){ Node fast = head; Node slow = head; if(fast == null){ return false; } while(fast != null && fast.next != null){ fast = fast.next.next; // 快指针一次走两步 slow = slow.next; // 慢指针一次走一步 if(fast == slow) return true; } return !(fast == null || fast.next == null);}
阅读全文
0 0
- 数据结构之链表及相关算法
- 数据结构-单向链表相关操作算法
- 数据结构与算法概述及链表的相关知识回顾
- 稀疏矩阵的数据结构及相关算法
- 数据结构:图相关概念及遍历算法
- 算法及数据结构之---链表
- 链表相关的算法题大汇总 — 数据结构之链表奇思妙想
- 链表相关的算法题大汇总 — 数据结构之链表奇思妙想
- 链表相关的算法题大汇总 (1)— 数据结构之链表奇思妙想
- 链表相关的算法题大汇总(2) — 数据结构之链表奇思妙想
- 链表相关的算法题大汇总 — 数据结构之链表奇思妙想
- 数据结构与算法学习之栈及栈的相关操作
- 数据结构与算法学习之队列及队列的相关操作
- 数据结构与算法学习之二叉树及二叉树的相关操作
- 数据结构与算法学习之二叉排序树及二叉排序树的相关操作
- 数据结构之链表——环相关问题及解决思路汇总
- 数据结构:单链表操作之如何判断链表是否带环及相关操作
- 数据结构之串13初谈串及相关
- 虚拟机字节码执行引擎
- AFNetWorking获取状态码
- 在 github 上添加 SSH key 的方法
- RecyclerView滑动到指定位置,并指定位置在顶部
- 排序算法之插入排序
- 数据结构之链表及相关算法
- DedeCMS5.7SP1系统/plus/download.php出现url重定向漏洞
- Android图表库MPAndroidChart(五)——自定义MarkerView实现选中高亮
- grep 匹配行中的一部分 分析 字符串中 -l 之类的flag标志
- IntelliJ IDEA 2017.3创建Web项目,解决无自动生成web.xml问题
- 使用Hive-JDBC遇到的一些问题解决
- 架构设计EIT造型下的android框架(一)
- JPQL查询实例
- Hadoop入门之HDFS操作