链表

1、反转链表

    public ListNode reverseLinkedList(ListNode head) {
        ListNode next = null;
        ListNode pre = null;
        while (head != null) {
            next = head.next;
            head.next = pre;
            pre = head;
            head = next;
        }
        return pre;
    }

2、判断单向链表是否有环

 2.1、使用HashSet

 在遍历过程中,使用一个hashSet集合来保存已经遍历过的节点 ,在遍历下一个节点的时候,判断这个节点是否在集合中,如果在这个集合中说明出现环了。

 2.2、使用快慢指针

 使用一个快指针,一个慢指针,快指针一次走两步,慢指针一次走一步,如果链表中有环,那么快指针和慢指针最后会相遇。
 还可以继续找到环的入口,相遇之后呢,快指针回到初始节点,一次走一步,那么最后快指针和慢指针最后会在链表环的入口相遇。

    public boolean isLoop(ListNode head) {
        if (head == null || head.next == null) {
            return false;
        }
        ListNode fast = head.next.next;
        ListNode slow = head.next;
        while (slow != fast) {
            if (fast == null || fast.next == null || fast.next.next == null) {
                return false;
            }
            fast = fast.next.next;
            slow = slow.next;
        }
        return true;
    }
 //如果没有环,返回null
    //如果有环,返回第一个入环的节点
    public static ListNode getLoopNode2(ListNode head) {
        if (head == null || head.next == null || head.next.next == null) {
            return null;
        }

        ListNode fast = head.next.next; //快指针一次两步
        ListNode slow = head.next; //慢指针一次一步

        while (fast != slow) { 
            if (fast == null || fast.next == null || fast.next.next == null) {  //如果到达链表结束,表示不可能有环
                return null;
            }
            slow = slow.next;
            fast = fast.next.next;
        }

        fast = head; //fast回到head,一次只走一步,再次遇到slow,即为入环节点

        while (fast != slow) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }

3、拷贝有随机指针的链表

有随机指针的链表

 3.0、使用老节点后新增、处理rand,拆分新旧节点

 1、第一遍遍历旧链表、再每个节点后插入新的拷贝的节点,先不管rand


step1

 2、第二遍、一次遍历两个节点,为新的拷贝节点处理rand指针

cur.next.rand = cur.rand == null ? null : cur.rand.next;
step2

 3、拆分新旧节点

 public Node copyLinkedList(Node head) {
        if (head == null) {
            return null;
        }
        Node cur = head;
        //1、在原来的链表中插入新的节点,1->1'->2->2'->3->3'->null
        while (cur != null) {
            Node next = cur.next;
            cur.next = new Node(cur.val);
            cur.next.next = next;
            cur = next;
        }
        cur = head;
        //2、给新节点添加rand指针
        while (cur != null) {
            cur.next.rand = cur.rand == null ? null : cur.rand.next;
            cur = cur.next.next;
        }
        //3、把新旧节点分离
        Node newHead = head.next;
        Node newTail = head.next;
        cur = head;
        while (cur != null) {
            Node next = cur.next.next;
            cur.next = next;
            newTail.next = next == null ? null : next.next;
            cur = next;
            newTail = newTail.next;
        }
        return newHead;
    }

 3.1、使用HashMap

 第一遍遍历旧链表,往Map中添加元素,Map的key是原来的节点,value是新的复制节点
 第二遍遍历旧链表,根据旧链表的关系,处理新链表的next,rand关系。

    public Node copyLinkedList(Node head) {
        if (head == null) {
            return null;
        }
        HashMap<Node, Node> map = new HashMap<>();
        Node cur = head;
        //1、把原始节点,和拷贝节点放入Map
        while (cur != null) {
            map.put(cur, new Node(cur.val));
            cur = cur.next;
        }
        cur = head;
        //2、处理新的节点的连接关系
        while (cur != null) {
            Node copy = map.get(cur);
            copy.next = map.get(cur.next);
            copy.rand = map.get(cur.rand);
            cur = cur.next;
        }
        return map.get(head);
    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 基本概念 链表的含义: 链表是一种用于存储数据集合的数据结构,具有以下属性 相邻元素之间通过指针相连 最后一个元素...
    古剑诛仙阅读 1,026评论 0 3
  • 搞懂单链表常见面试题 Hello 继上次的 搞懂基本排序算法,这个一星期,我总结了,我所学习和思考的单链表基础知识...
    醒着的码者阅读 4,626评论 1 45
  • 判断两个单链表是否相交以及相交的情况下求第一个相交节点,两个链表可以有环,也可以无环。因此我们可以从以下几个步骤分...
    Yufail阅读 1,824评论 2 5
  • 题目一 代码如下: 题目二 代码如下: 题目三 思路1:遍历一边链表,将链表每个节点的value压入栈中,再重头进...
    憨憨二师兄阅读 701评论 0 3
  • 介绍 1、链表问题算法难度不高,但考察代码实现能力。2、链表和数组都是一种线性结构。①数组是一段连续的存储空间。②...
    雨住多一横阅读 369评论 0 0