数据结构之链表

单向链表反转

复制代码
// 通过递归的方式找到最后的节点
// 指向前面的节点,这样可以不破坏之前的指向回溯回去
public static Node reverseLinkedNode(Node head) {
    if (head == null || head.next == null) {
        // 返回最后的节点
        return head;
    }
    Node nextNode = reverseLinkedNode(head.next);
    nextNode.next = head;
    head.next = null;
    return head;
}

// 直接反转

复制代码
public static Node reverse(Node head) {
    Node next = null;
    Node pre = null;
    while (head != null) {
        next = head.next;
        head.next = pre;
        pre = head;
        head = next;
    }
    return pre;
}

题目中nextNode永远会返回head的下一个节点

双向链表反转

用两个node记录下来当前节点的前后环境

复制代码
public static DDNode reverse(DDNode head) {
    DDNode pre = null;
    DDNode next = null;
    while(head != null) {
        next = head.next;
        head.next = pre;
        head.pre = next;
        pre = head;
        head = next;
    }
    return pre;
}

打印两个有序列表的公共部分

主要思想和归并排序有些像,因为是有序的链表

所以同时从头开始对比大小,小的一方就next,直到相等后同时记录后,next

有任何一方遍历到结束就返回结果

复制代码
public static List<Integer> getOverlap(Node n1, Node n22) {
    if (n1 == null || n22 == null) {
        return null;
    }
    List<Integer> result = new ArrayList<>();
    int v1, v2;
    while(n1 != null && n22 != null) {
        v1 = n1.value;
        v2 = n22.value;
        if (v1 == v2) {
            result.add(v1);
            n1 = n1.next;
            n22 = n22.next;
        } else if (v1 > v2) {
            n22 = n22.next;
        } else {
            n1 = n1.next;
        }
    }
    return result;
}

判断是不是回文链表

回文链表:1->2->3->2->1

思路:用栈记录后出站和原来列表对比,因为栈是逆序,如果逆序和链表仍然相等

则是回文链表

复制代码
public static boolean isMirror(Node head) {
    Stack<Node> stack = new Stack<>();
    Node node = head;
    while (head != null) {
        stack.push(head);
        head = head.next;
    }
    boolean result = true;
    while (node != null) {
        if (node.value != stack.pop().value) {
            System.out.println(node.value);
            result = false;
            return result;
        }
        node = node.next;
    }
    return result;
}

链表排序

把链表转换为数组,然后进行快速排序,再转换为链表

复制代码
public static Node nodeSort(Node head) {
    List<Node> nodes = new ArrayList<>();
    while (head != null) {
        nodes.add(head);
        head = head.next;
    }
    if (nodes.size() <= 1) {
        return head;
    }
    return qs(nodes, 0, nodes.size() - 1);
}
// 返回头节点
public static Node qs(List<Node> nodes, int low, int high) {
    if (high <= low) {
        return null;
    }
    int partition = partition(nodes, low, high);
    qs(nodes, low, partition - 1);
    qs(nodes, partition + 1, high);
    for(int i = 0; i< nodes.size();i++) {
        if (i + 1 < nodes.size()) {
            nodes.get(i).next = nodes.get(i + 1);
        } else {
            nodes.get(i).next = null;
        }
    }
    return nodes.get(0);
}
public static int partition(List<Node> nodes, int low, int high) {
    Node standard = nodes.get(low);
    while (low < high) {
        while (low < high && nodes.get(high).value >= standard.value) {
            high --;
        }
        swap(nodes, low, high);
        while (low < high && nodes.get(low).value <= standard.value) {
            low ++;
        }
        swap(nodes, high, low);
    }
    nodes.set(low, standard);
    return low;
}

判断链表是否有环,并返回入环节点

一. 使用HashSet存储如果存在重复存储元素则有环

复制代码
public static Node getCycleNode(Node head) {
    HashSet<Node> hashSet = new HashSet<>();
    while (head != null) {
        if (!hashSet.contains(head)) {
            hashSet.add(head);
        } else {
            return head;
        }
        head = head.next;
    }
    return null;
}

二. 使用快慢指针

快指针一次走两个,慢指针一次一步,

当快慢指针相遇时说明有环,此时让快指针回到原点,再相遇时就是入环节点

复制代码
public static Node getCycleNode2(Node head) {
    if (head == null || head.next == null || head.next.next == null) {
        return null;
    }
    Node quick = head.next;
    Node slow = head.next.next;
    while (quick != slow) {
        quick = quick.next;
        slow = slow.next.next;
    }
    quick = head;
    while (quick != slow) {
        quick = quick.next;
        slow = slow.next;
    }
    return quick;
}
相关推荐
CCF_NOI.5 分钟前
(普及−)B3629 吃冰棍——二分/模拟
数据结构·c++·算法
神的孩子都在歌唱1 小时前
3423. 循环数组中相邻元素的最大差值 — day97
java·数据结构·算法
艾莉丝努力练剑3 小时前
【C语言】学习过程教训与经验杂谈:思想准备、知识回顾(三)
c语言·开发语言·数据结构·学习·算法
汤姆爱耗儿药8 小时前
专为磁盘存储设计的数据结构——B树
数据结构·b树
许小燚16 小时前
线性表——双向链表
数据结构·链表
qqxhb19 小时前
零基础数据结构与算法——第四章:基础算法-排序(上)
java·数据结构·算法·冒泡·插入·选择
晚云与城19 小时前
【数据结构】顺序表和链表
数据结构·链表
FirstFrost --sy20 小时前
数据结构之二叉树
c语言·数据结构·c++·算法·链表·深度优先·广度优先
Yingye Zhu(HPXXZYY)21 小时前
Codeforces 2021 C Those Who Are With Us
数据结构·c++·算法
liulilittle1 天前
LinkedList 链表数据结构实现 (OPENPPP2)
开发语言·数据结构·c++·链表