![](https://img-blog.csdnimg.cn/20201014180756928.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
数据结构练习
LXL7868
这个作者很懒,什么都没留下…
展开
-
【数据结构练·JAVA】反转一个单链表
public Node reverseList(){ Node cur = this.head;//定义 cur 指向头结点 Node pre = null; Node reverseHead = null;//reverseHead 为逆置后的头结点 //使用 cur 遍历整个链表 ...原创 2019-04-23 12:57:47 · 204 阅读 · 0 评论 -
【数据结构练·JAVA】给定一个带有头结点 head 的非空单链表,返回链表的中间结点。如果有两个中间结点,则返回第二个 中间结点。
无论链表节点个数为奇数还是偶数,返回都是链表长度的一半的节点所对应的数值即可public int middleNode() { Node cur = this.head; int len = getLength() / 2;//获得链表长度的一半 //让 cur 走过连表长度的一半 for (int i = 0; i < ...原创 2019-04-23 13:14:30 · 1357 阅读 · 0 评论 -
【数据结构练·JAVA】输入一个链表,输出该链表中倒数第k个结点。
public int findKthToTail(int k) { //定义fast、slow两个指向头结点 Node fast = this.head; Node slow = this.head; //判断节点k的合法性 if (fast == null || k <= 0 || k > getLen...原创 2019-04-23 13:25:48 · 262 阅读 · 0 评论 -
【数据结构练·JAVA】 编写代码,以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前 。
链表分割为下图:实现代码: public Node partition(int x) { Node beforeStart = null; Node beforeEnd = null; Node afterStart = null; Node afterEnd = null; Node pHead = t...原创 2019-04-23 13:45:50 · 624 阅读 · 0 评论 -
【数据结构练·JAVA】 给定一个链表,判断链表中是否有环。
代码实现: //首先构造一个环 public void createCircle(){ Node cur = this.head; while (cur.next != null){ cur = cur.next; } cur.next = this.head.next; } ...原创 2019-04-23 13:52:18 · 408 阅读 · 0 评论 -
【数据结构练·JAVA】给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 NULL。
代码实现:public Node detectCycle() { Node fast = this.head; Node slow = this.head; //判断是否有环,若有环则fast = slow,退出循环 while (fast != null && fast.next.next != null) {...原创 2019-04-23 14:00:37 · 1076 阅读 · 0 评论