밑에서 k번째 노드를 찾아서 돌아오기
1251 단어 LinkedList
package linkedlist;
import javax.swing.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
/**
* Created by Administrator on 2015/11/6 0006.
*/
public class Hashuan {
public static void main(String[] args) {
Node h1=new Node(0);
Node head1=h1;
Node t1=new Node(1);
Node t2=new Node(2);
Node t3=new Node(3);
Node t4=new Node(4);
head1.next=t1;
t1.next=t2;
t2.next=t3;
t3.next=t4;
t4.next=null;
//System.out.println(panduan(head1));
int k=2;
System.out.println(daoshuK(head1,2).value);
}
private static Node daoshuK(Node head1,int k) {
if(head1==null)
return null;
Node p=head1;
Node q=head1;
while (k>0){
p=p.next;
if(p==null)
return null;
k--;
}
while (p!=null){
p=p.next;
q=q.next;
}
return q;
}
static class Node{
Node next;
int value;
public Node(int value) {
this.value = value;
next=null;
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Java 컬렉션 프레임워크LinkedList 상세 정보 및 인스턴스Java 컬렉션 프레임워크LinkedList 상세 정보 LinkedList 정의 LinkedList 개요 LinkedList는 양방향 체인 테이블로 이루어지며 중복을 허용합니다.(아래 Node 구현) 헤더 및 끝 바...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.