找到单链表中的倒数第K个节点,并打印输出节点。两段代码,思路都是相似的。
一、代码1
public class SingleLinkedListDemo {
public static void main(String[] args) {
System.out.println("开始测试... ... ... ... ...");
HeroNode hero1 = new HeroNode(1, "宋江", "及时雨");
HeroNode hero2 = new HeroNode(2, "卢俊义", "玉麒麟");
HeroNode hero3 = new HeroNode(3, "吴用", "智多星");
HeroNode hero4 = new HeroNode(4, "公孙胜", "入云龙");
HeroNode hero5 = new HeroNode(5, "关胜", "大刀");
SingleLinkedList singlelinkedlist1 = new SingleLinkedList();
singlelinkedlist1.add(hero1);
singlelinkedlist1.add(hero3);
singlelinkedlist1.add(hero2);
singlelinkedlist1.add(hero4);
singlelinkedlist1.add(hero5);
singlelinkedlist1.list();
HeroNode res = singlelinkedlist1.findLastIndexNode(singlelinkedlist1.getHead(), 2);
System.out.println("res = " + res);
}
}
class SingleLinkedList{
private HeroNode head = new HeroNode(0, "", "");
public HeroNode getHead() {
return head;
}
public void add(HeroNode heroNode) {
HeroNode temp = head;
while(true) {
if(temp.next == null) {
break;
}
temp = temp.next;
}
temp.next = heroNode;
}
public void addByOrder(HeroNode heroNode) {
HeroNode temp = head;
boolean flag = false;
while(true) {
if(temp.next == null) {
break;
}
if(temp.next.no > heroNode.no) {
break;
}
else if(temp.next.no == heroNode.no) {
flag = true;
break;
}
temp = temp.next;
}
if(flag) {
System.out.printf("待插入的英雄(排名 = %d)不存在!\n", heroNode.no);
}else {
heroNode.next = temp.next;
temp.next = heroNode;
}
}
public void list() {
if(head.next == null) {
System.out.println("链表为空");
return;
}
HeroNode temp = head.next;
while(true) {
if(temp == null) {
break;
}
System.out.println(temp);
temp = temp.next;
}
}
public void updata(HeroNode newHeroNode) {
if(head.next == null) {
System.out.println("链表为空!");
return;
}
HeroNode temp = head.next;
boolean flag = false;
while(true) {
if(temp == null) {
break;
}
if(temp.no == newHeroNode.no) {
flag = true;
break;
}
temp = temp.next;
}
if(flag) {
temp.name = newHeroNode.name;
temp.nickname = newHeroNode.nickname;
}else {
System.out.printf("没有找到排名为%d的英雄\n", newHeroNode.no);
}
}
public void del(int no) {
HeroNode temp = head;
boolean flag = false;
while(true) {
if(temp.next == null) {
break;
}
if(temp.next.no == no) {
flag = true;
break;
}
temp = temp.next;
}
if(flag) {
temp.next = temp.next.next;
}else {
System.out.printf("要删除的%d节点不存在!", no);
}
}
public static int getLength(HeroNode head) {
if(head.next == null) {
return 0;
}
int length = 0;
HeroNode cur = head.next;
while(cur != null) {
length++;
cur = cur.next;
}
return length;
}
public static HeroNode findLastIndexNode(HeroNode head, int index) {
if(head.next == null) {
return null;
}
int size = getLength(head);
if(index <= 0 || index > size) {
return null;
}
HeroNode cur = head.next;
for(int i = 0; i < size - index; i++) {
cur = cur.next;
}
return cur;
}
}
class HeroNode{
public int no;
public String name;
public String nickname;
public HeroNode next;
public HeroNode(int no, String name, String nickname) {
this.no = no;
this.name = name;
this.nickname = nickname;
}
@Override
public String toString() {
return "HeroNode [排名 = " + no + ", 姓名 = " + name + ", 绰号 = " + nickname + "]";
}
}
二、结果1(依次是倒数第2、第1、第6、第0、第-1个节点)
开始测试... ... ... ... ...(倒数第2个节点)
HeroNode [排名 = 1, 姓名 = 宋江, 绰号 = 及时雨]
HeroNode [排名 = 3, 姓名 = 吴用, 绰号 = 智多星]
HeroNode [排名 = 2, 姓名 = 卢俊义, 绰号 = 玉麒麟]
HeroNode [排名 = 4, 姓名 = 公孙胜, 绰号 = 入云龙]
HeroNode [排名 = 5, 姓名 = 关胜, 绰号 = 大刀]
res = HeroNode [排名 = 4, 姓名 = 公孙胜, 绰号 = 入云龙]
开始测试... ... ... ... ...(倒数第1个节点)
HeroNode [排名 = 1, 姓名 = 宋江, 绰号 = 及时雨]
HeroNode [排名 = 3, 姓名 = 吴用, 绰号 = 智多星]
HeroNode [排名 = 2, 姓名 = 卢俊义, 绰号 = 玉麒麟]
HeroNode [排名 = 4, 姓名 = 公孙胜, 绰号 = 入云龙]
HeroNode [排名 = 5, 姓名 = 关胜, 绰号 = 大刀]
res = HeroNode [排名 = 5, 姓名 = 关胜, 绰号 = 大刀]
开始测试... ... ... ... ...(倒数第6个节点)
HeroNode [排名 = 1, 姓名 = 宋江, 绰号 = 及时雨]
HeroNode [排名 = 3, 姓名 = 吴用, 绰号 = 智多星]
HeroNode [排名 = 2, 姓名 = 卢俊义, 绰号 = 玉麒麟]
HeroNode [排名 = 4, 姓名 = 公孙胜, 绰号 = 入云龙]
HeroNode [排名 = 5, 姓名 = 关胜, 绰号 = 大刀]
res = null
开始测试... ... ... ... ...(倒数第0个节点)
HeroNode [排名 = 1, 姓名 = 宋江, 绰号 = 及时雨]
HeroNode [排名 = 3, 姓名 = 吴用, 绰号 = 智多星]
HeroNode [排名 = 2, 姓名 = 卢俊义, 绰号 = 玉麒麟]
HeroNode [排名 = 4, 姓名 = 公孙胜, 绰号 = 入云龙]
HeroNode [排名 = 5, 姓名 = 关胜, 绰号 = 大刀]
res = null
开始测试... ... ... ... ...(倒数第-1个节点)
HeroNode [排名 = 1, 姓名 = 宋江, 绰号 = 及时雨]
HeroNode [排名 = 3, 姓名 = 吴用, 绰号 = 智多星]
HeroNode [排名 = 2, 姓名 = 卢俊义, 绰号 = 玉麒麟]
HeroNode [排名 = 4, 姓名 = 公孙胜, 绰号 = 入云龙]
HeroNode [排名 = 5, 姓名 = 关胜, 绰号 = 大刀]
res = null
代码2(LeetCode中有这个题目)
public class MyTest {
public static void main(String[] args) {
ListNode listnode1 = new ListNode(2);
ListNode listnode2 = new ListNode(3);
ListNode listnode3 = new ListNode(4);
ListNode listnode4 = new ListNode(5);
LinkedList linkedlist = new LinkedList();
linkedlist.addListNode(listnode1);
linkedlist.addListNode(listnode2);
linkedlist.addListNode(listnode3);
linkedlist.addListNode(listnode4);
linkedlist.list(linkedlist.getHead());
System.out.println("------------------------------");
ListNode kth = linkedlist.findKthNode(linkedlist.getHead(), 2);
linkedlist.list(kth);
}
}
class ListNode{
public int val;
public ListNode next;
public ListNode(int val) {
this.val = val;
}
@Override
public String toString() {
return "" + val;
}
}
class LinkedList{
private ListNode head = new ListNode(1);
public ListNode getHead() {
return head;
}
public void addListNode(ListNode listnode) {
ListNode temp = head;
while(true) {
if(temp.next == null) {
temp.next = listnode;
break;
}
temp = temp.next;
}
}
public ListNode findKthNode(ListNode head, int k) {
if(head == null) {
return null;
}
int length = 0;
ListNode temp = head;
while(temp != null) {
length++;
temp = temp.next;
}
if(k <= 0 || k > length) {
return null;
}
ListNode cur = head;
for(int i = 0; i < length - k; i++) {
cur = cur.next;
}
return cur;
}
public void list(ListNode head){
if(head == null) {
System.out.println(head);
return;
}
ListNode temp = head;
System.out.print("[");
while(true) {
if(temp.next == null) {
System.out.print(temp);
break;
}
if(temp.next != null) {
System.out.print(temp + ",");
}
temp = temp.next;
}
System.out.println("]");
}
}
二、结果1(依次是倒数第2、第1、第6、第0、第-1个节点)
[1,2,3,4,5]
------------------------------
[4,5]
[1,2,3,4,5]
------------------------------
[5]
[1,2,3,4,5]
------------------------------
null
[1,2,3,4,5]
------------------------------
null
[1,2,3,4,5]
------------------------------
null