题解 | #输出单向链表中倒数第k个结点#

输出单向链表中倒数第k个结点

http://www.nowcoder.com/practice/54404a78aec1435a81150f15f899417d

import java.util.*;

public class Main{ private static Node head; public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int num = sc.nextInt(); head = new Node(); for(int i = 0; i < num;i++){ int val = sc.nextInt(); //指针指向下一个元素 head.next = new Node(val, head.next); } int target = sc.nextInt(); for(int i =0; i < target;i++){ //找到下一个元素 head = head.next; } System.out.println(head.value); } } } class Node{ Node node; int value; Node next; public Node(){

}
public Node(int value,Node next){
    this.value = value;
    this.next = next;
}

}

全部评论

相关推荐

点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-01 11:47
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务