链表篇

作者: 王鑫鑫_d516 | 来源:发表于2019-02-28 08:34 被阅读0次

有环链表判断,快慢指针

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public boolean hasCycle(ListNode head) {
        
        if (head == null || head.next == null) {
            return false;
        }
        
        ListNode fast = head.next.next, slow = head.next;
        
        while (fast != null && fast.next != null && fast != slow) {
            slow = slow.next;
            fast = fast.next.next;
        }
        
        return fast == slow;
        
    }
}

通用克隆数据结构方法

/*
// Definition for a Node.
class Node {
    public int val;
    public Node next;
    public Node random;

    public Node() {}

    public Node(int _val,Node _next,Node _random) {
        val = _val;
        next = _next;
        random = _random;
    }
};
*/
class Solution {
    public Node copyRandomList(Node head) {
                
        if (head == null) return null;
        
        Map<Node, Node> map = new HashMap<>();
        
        // copy nodes
        Node cur = head;
        while (cur != null) {
            map.put(cur, new Node(cur.val));
            cur = cur.next;
        }
        
        // copy link
        cur = head;
        while (cur != null) {
            map.get(cur).next = map.get(cur.next);
            map.get(cur).random = cur.random == null ? null : map.get(cur.random);
            cur = cur.next;
        }
        
        return map.get(head);
    }
}

Tricky 方法

/*
// Definition for a Node.
class Node {
    public int val;
    public Node next;
    public Node random;

    public Node() {}

    public Node(int _val,Node _next,Node _random) {
        val = _val;
        next = _next;
        random = _random;
    }
};
*/
class Solution {
    public Node copyRandomList(Node head) {
                
        if (head == null) return null;
        
        // Copy node and put into cur.next
        Node cur = head;
        while (cur != null) {
            Node next = cur.next;
            Node copy = new Node(cur.val);
            cur.next = copy;
            copy.next = next;
            cur = next;
        }

        // Link the random
        cur = head;
        while (cur != null) {
            cur.next.random = cur.random == null ? null : cur.random.next;
            cur = cur.next.next;
        }
        
        // Sepreate two linkedlist
        cur = head;
        Node copyHead = head.next;
        
        while (cur != null) {
            Node copy = cur.next;
            Node next = copy.next;
            cur.next = next;
            copy.next = next == null ? null : next.next;
            cur = next;
        }
        return copyHead;
    }
}

相关文章

  • 大话数据结构之链表(二)

    上一篇《链表概念篇》中, 主要给小伙伴们讲述了什么是链表? 为什么链表是线性结构? 链表的操作是什么? 链表操作的...

  • 总结

    Android篇 数据结构与算法顺序表 - ArrayList源码链表 - 单向链表、双向链表 - LinkedL...

  • 链表篇-反转链表

    题目 输入一个链表,反转链表。 1、思路 利用三个指针,分别指向当前节点、当前节点前一个节点、当前节点后一个节点,...

  • 链表结束篇:链表的五种常见操作

    链表结束篇:链表的五种常见操作 单链表翻转 检测一个链表中是否有环 两个有序的链表合并 删除链表倒数第 n 个结点...

  • 链表篇

    有环链表判断,快慢指针 通用克隆数据结构方法 Tricky 方法

  • 链表篇

    从尾到头打印链表Q:不希望打印时修改内容,怎么办?A:遍历的时候从头到尾,而输出的顺序是从尾到头,典型的“后进先出...

  • js实现链表(很有意思)

    链表有单向链表、双向链表和循环链表,此篇文章只讲解单向链表,另外两种会在下一篇文章中补充,要真正理解和使用链表的话...

  • 第2篇 C++ 数据结构-单链表(多图杀猫)

    如果你对链表《第2篇 C++ 数据结构-链表概述》请先阅读前文,我们前一篇已经罗列单链表的类接口清单,本篇会依据接...

  • 数据结构与算法学习-双向链表

    双向链表 在上一篇单链表中已经提到了双向链表,其实单链表实现时候,双向链表相对容易多了,只不过对每个节点多了一个前...

  • 迭代实现-js-v1.0.0

    从字面量(如for循环)到迭代(递归) 数组篇 某栈篇 队列篇 链表篇

网友评论

      本文标题:链表篇

      本文链接:https://www.haomeiwen.com/subject/iumcuqtx.html