https://leetcode-cn.com/problems/copy-list-with-random-pointer/submissions/
    image.png
    image.png
    image.png
    image.png

    1. public Node copyRandomList(Node head) {
    2. if (head == null) return null;
    3. Node cur = head;
    4. Node next = null;
    5. while (cur != null) {
    6. next = cur.next;
    7. cur.next = new Node(cur.val);
    8. cur.next.next = next;
    9. cur = next;
    10. }
    11. cur = head;
    12. while (cur != null) {
    13. next = cur.next.next;
    14. cur.next.random = cur.random == null ? null : cur.random.next;
    15. cur = next;
    16. }
    17. Node ans = head.next;
    18. cur = head;
    19. while (cur != null) {
    20. next = cur.next.next;
    21. cur.next.next = next == null ? null : next.next;
    22. cur.next = next;
    23. cur = next;
    24. }
    25. return ans;
    26. }