直接上实现代码
public static void reverseList(HeroNode head
){
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
;
}
最主要的是while循环里的逻辑
while(cur
!= null
){
next
= cur
.next
;
cur
.next
= reverseHead
.next
;
reverseHead
.next
= cur
;
cur
= next
;
}
这里一定要画图来慢慢理解,首先一个图是原链表,另一个是新链表的头部,边画图变理解着写逻辑
第一步先保留一下原链表的第二个位置head.next.next也就是代码里的cur.next,如果不保留的话取出来后原链表就断掉了
第二步,让取出来的cur的右边先next指向reverseHead.next
左边是reverseHead.next = cur
然后把next赋给cur
在循环完链表后把链表的头部更换
head
.next
= reverseHead
.next
;
下面是完整代码(如果只看反转可以找到reverseList()方法)
package DataStructures
.LinkedList
;
public class SingleLinedListDemo {
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 linkedList
= new SingleLinkedList();
linkedList
.addByOrder(hero1
);
linkedList
.addByOrder(hero4
);
linkedList
.addByOrder(hero2
);
linkedList
.addByOrder(hero3
);
linkedList
.addByOrder(hero3
);
linkedList
.list();
reverseList(linkedList
.getHead());
linkedList
.list();
}
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
fidLastIndexNode(HeroNode head
, int index
){
if (head
.next
== null
){
return null
;
}
int size
= getLength(head
);
if (index
<=0 || index
> size
){
return null
;
}
HeroNode temp
= head
.next
;
for (int i
=0; i
<size
- index
; i
++){
temp
= temp
.next
;
}
return temp
;
}
public static void reverseList(HeroNode head
){
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
;
}
}
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 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 的节点,不能修改",newHeroNode
.no
);
}
}
public void delete(int no
){
if (head
.next
== null
){
System
.out
.println("链表为空,不能删除");
return;
}
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("链表里没有no为:%d 的节点\n",no
);
}
}
public void list(){
if (head
.next
== null
){
return;
}
HeroNode temp
= head
.next
;
while(true){
if (temp
== null
){
break;
}
System
.out
.println(temp
);
temp
= temp
.next
;
}
}
}
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=" + no
+
", name='" + name
+ '\'' +
", nickName='" + nickName
+ '\'' +
'}';
}
}
转载请注明原文地址: https://mac.8miu.com/read-71739.html