다음과 같은 다중 방향 연결 목록 구조(동영상 튜토리얼 권장 사항:
java 강좌) public class Node{
public int value;
public Node next;
public Node rand;
public Node(int data){
this.value = data;
}
}
방법 1: HashMap 구조 사용
public class CopyFromMultiNode { public static void main(String[] args){ int[] array = {12,3,4,5,6,77,6,54,56,6,7,87,15,15,15}; //数组转Node Node head = array2node(array); Node help = head; System.out.print("处理前 "); while(help != null){ System.out.print(help.value + " "); help = help.next; } //使用HashMap结构 Node res = copyFromRand1(head); System.out.println(); System.out.print("处理后结果:"); while(res != null){ System.out.print(res.value+" "); res = res.next; } } //使用HashMap结构 public static Node copyFromRand1(Node head){ Node cur = head; HashMap<Node, Node> map = new HashMap<>(); while(cur != null){ map.put(cur, new Node(cur.value)); cur = cur.next; } cur = head; while(cur != null){ map.get(cur).next = map.get(cur.next); map.get(cur).rand = map.get(cur.rand); cur = cur.next; } return map.get(head); } //数组转Node功能,供测试使用 public static Node array2node(int[] array){ Node head = new Node(array[0]); Node cur = head; for(int i=1; i<array.length; i++){ cur.next = new Node(array[i]); cur = cur.next; } return head; } //基础node节点结构 public static class Node{ public int value; public Node next; public Node rand; public Node(int data){ this.value = data; } } }
2. 다른 구조 없이 여러 가지 효과적인 변수 방법 사용
//使用几个有效变量方法 //替换方法一的copyFromRand1方法 public static Node copyFromRand2(Node head){ Node next = null; Node cur = head; //1 -> 2 -> 3 -> 4 ==> 1 -> 1` -> 2 -> 2` -> 3 -> 3` -> 4 -> 4 //完成链表拼接 while(cur != null){ next = cur.next; cur.next = new Node(cur.value); cur.next.next = next; cur = next; } cur = head; Node curCopy = null; //添加Node的rand值 while(cur != null){ next = cur.next.next; curCopy = cur.next.next; curCopy = cur.rand != null? cur.rand.next: null; cur = next; } Node res = head.next; cur = head; //拆分 // 1 -> 1` -> 2 -> 2` -> 3 -> 3` -> 4 -> 4 // ==> 1 -> 2 -> 3 -> 4 和 1`-> 2`-> 3`-> 4` while(cur != null){ next = cur.next.next; curCopy = cur.next; cur.next = next; curCopy.next = next != null ? next.next:null; cur = next; } return res; }
관련 튜토리얼 권장:
Java 시작하기위 내용은 Java에서 다중 연결 목록을 복사하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

Video Face Swap
완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

인기 기사

뜨거운 도구

안전한 시험 브라우저
안전한 시험 브라우저는 온라인 시험을 안전하게 치르기 위한 보안 브라우저 환경입니다. 이 소프트웨어는 모든 컴퓨터를 안전한 워크스테이션으로 바꿔줍니다. 이는 모든 유틸리티에 대한 액세스를 제어하고 학생들이 승인되지 않은 리소스를 사용하는 것을 방지합니다.

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

DVWA
DVWA(Damn Vulnerable Web App)는 매우 취약한 PHP/MySQL 웹 애플리케이션입니다. 주요 목표는 보안 전문가가 법적 환경에서 자신의 기술과 도구를 테스트하고, 웹 개발자가 웹 응용 프로그램 보안 프로세스를 더 잘 이해할 수 있도록 돕고, 교사/학생이 교실 환경 웹 응용 프로그램에서 가르치고 배울 수 있도록 돕는 것입니다. 보안. DVWA의 목표는 다양한 난이도의 간단하고 간단한 인터페이스를 통해 가장 일반적인 웹 취약점 중 일부를 연습하는 것입니다. 이 소프트웨어는

에디트플러스 중국어 크랙 버전
작은 크기, 구문 강조, 코드 프롬프트 기능을 지원하지 않음

VSCode Windows 64비트 다운로드
Microsoft에서 출시한 강력한 무료 IDE 편집기
