翻转图演示
定义一个节点reverseHead = new HeroNode();
从头到尾遍历原来的链表,没遍历一个节点,就将其取出,并放在新的链表的最前端;
原来链表的head.next = reverseHead.next;
理解:
蓝色的线是之前的链表节点之间的联系
橘色的线是我要改变成的节点之间的联系
//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;
}
main方法加入
System.out.println("翻转后的链表");
HeroNode head = singleLinkedList.getHead();
reverseList(head);
singleLinkedList.list();
运行结果
至此实现了单向链表的翻转
package com.chad.lambda;
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("翻转后的链表");
HeroNode head = singleLinkedList.getHead();
reverseList(head);
singleLinkedList.list();
}
//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 + '\'' +
'}';
}
}