先将单链表翻转然后再链表
单链表的翻转见单链表的翻转
(这样做会破坏原来的单链表的一个结构【不建议这么操作】)
可以利用栈这个数据结构,将各个节点压入栈中,然后利用栈的先进后出的特点就实现了逆序打印的效果
栈的演示:
package com.chad.lambda;
import java.util.Stack;
public class TestStack {
public static void main(String[] args) {
//演示栈的基本使用
Stack<String> stack = new Stack<String>();
//入栈
stack.add("jack");
stack.add("tom");
stack.add("chad");
stack.add("bunny");
//出栈
while(stack.size() > 0){
System.out.println(stack.pop());
}
}
}
我们发现出栈的数据顺序和进栈的顺序正好是相反的
编写方法reversePrint()
//使用方式二实现逆序打印
public static void reversePrint(HeroNode head){
if(head.next == null){
return;//空链表不用打印
}
Stack<HeroNode> stack = new Stack<>();
HeroNode cur = head.next;
while (cur != null){
stack.push(cur);
cur = cur.next;
}
while (stack.size() > 0){
System.out.println(stack.pop());
}
}
//显示一把
singleLinkedList.list();
System.out.println(“翻转打印”);
reversePrint(singleLinkedList.getHead());
至此实现了单链表的逆序打印
package com.chad.lambda;
import java.util.Stack;
public class SingleLinkedListDemo {
public static void main(String[] args) {
//进行测试
HeroNode hero1 = new HeroNode(1,"宋江","及时雨");
HeroNode hero2 = new HeroNode(2,"卢俊义","玉麒麟");
HeroNode hero3 = new HeroNode(3,"吴用","智多星");
HeroNode hero4 = new HeroNode(4,"林冲","豹子头");
SingleLinkedList singleLinkedList = new SingleLinkedList();
//加入英雄
singleLinkedList.addByOrder(hero1);
singleLinkedList.addByOrder(hero4);
singleLinkedList.addByOrder(hero2);
singleLinkedList.addByOrder(hero3);
//显示一把
singleLinkedList.list();
System.out.println("翻转打印");
reversePrint(singleLinkedList.getHead());
}
//使用方式二实现逆序打印
public static void reversePrint(HeroNode head){
if(head.next == null){
return;//空链表不用打印
}
Stack<HeroNode> stack = new Stack<>();
HeroNode cur = head.next;
while (cur != null){
stack.push(cur);
cur = cur.next;
}
while (stack.size() > 0){
System.out.println(stack.pop());
}
}
//Tencent Interview Questions
public static void reverseList(HeroNode head){
//if LinkedList is null or just one node return
if( head.next == null || head.next.next == null){
return;
}
HeroNode cur = head.next;
HeroNode next = null;
HeroNode reverseHead = new HeroNode(0,"","");
//遍历原来的列表
while(cur != null){
next = cur.next;
cur.next = reverseHead.next;
reverseHead.next = cur;
cur = next;
}
head.next = reverseHead.next;
}
//Sina Interview Questions
public static HeroNode findLastNode(HeroNode head, int k){
if( head.next == null){
System.out.println("linkedList is Empty");
return null;
}
int length = getLength(head);
if(k<=0 || k> length){
System.out.println("The input number is not right");
return null;
}
//Define an auxiliary variable
HeroNode cur = head.next;
for (int i = 0; i < length - k; i++) {
cur = cur.next;
}
return cur;
}
//get singleLinkedList's node number(not head node)
public static int getLength(HeroNode head){
if (head.next == null){
System.out.println("The LinkList is Empty");
return 0;
}
int length = 0;
//定义一个辅助变量
HeroNode cur = head.next;
while(cur != null){
length++;
cur = cur.next;
}
return length;
}
}
//定义SingleLinkedList管理我们的英雄
class SingleLinkedList{
//初始化一个头节点,不存放具体数据
private HeroNode head = new HeroNode(0,"","");
public HeroNode getHead() {
return head;
}
//不考虑顺序,找到链表的最后节点,将最后的next指向新的节点
public void addHero(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){ //说明我的heroNode应该插入到这个temp的后面
break;
}else if (temp.next.no == heroNode.no){
flag = true;
break;
}
temp = temp.next; //后移
}
//判断flag的值
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 tmp = head.next;
while (true){
if(tmp.next == null){
System.out.println(tmp);
break;
}
System.out.println(tmp);
tmp = tmp.next;
}
}
}
class HeroNode{
public int no;
public String name;
public String nickName;
public HeroNode next; //指向下一个节点
//构造器
public HeroNode(int hNo,String hName,String hNickName) {
this.no = hNo;
this.name = hName;
this.nickName = hNickName;
}
//重写toString
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", name='" + name + '\'' +
", nickName='" + nickName + '\'' +
'}';
}
}