节点类:
class ListNode {
public int data; //data可以是任何类型
public ListNode next;
public ListNode(int data) {
this.data = data;
this.next = null;
}
}
class TestDemon1 {
public ListNode head; //定义头部
public TestDemon1() {
this.head = null;
}
//单链表
//尾插法
private void addFirst(int data) {
ListNode node=new ListNode(data);
if(this.head==null){ //头为空
this.head=node;
}else{
node.next=this.head;
this.head=node;
}
}
//反转单链表
public ListNode reserveList(){
ListNode cur=this.head;
ListNode pre=null;
ListNode newHead=null;
while (cur!=null){
ListNode curNext=cur.next;
if(curNext==null){
newHead=cur;
}else { //开始反转
cur.next=pre;
pre=cur;
cur=curNext;
}
}
return newHead;
}
//打印单链表
public void display() {
ListNode cur = this.head;
while (cur != null) {
System.out.print(cur.data + " ");
cur = cur.next;
}
System.out.println();
}
public void display2(ListNode newHead) {
ListNode cur = newHead;
while (cur != null) {
System.out.print(cur.data + " ");
cur = cur.next;
}
System.out.println();
}
public static void main(String[] args) {
TestDemon1 mySignalList = new TestDemon1();
mySignalList.addFirst(1);
mySignalList.addFirst(2);
mySignalList.addFirst(3);
mySignalList.addFirst(4);
mySignalList.addFirst(5);
mySignalList.display();
ListNode node=mySignalList.reserveList();
mySignalList.display2(node);
}
}
运行结果: