接上面的单向链表实现,单链表的反转。
/**
* 单链表反转
* @param headNode
*/
public void reverseList(Node headNode){
if (headNode.getNext() == null || headNode.getNext().getNext() == null){
return;
}
Node cur=headNode.getNext();
Node next=null;
Node reverseHeadNode=new Node(0,"");
while (cur!=null){
next=cur.getNext();
cur.setNext(reverseHeadNode.getNext());
reverseHeadNode.setNext(cur);
cur=next;
}
headNode.setNext(reverseHeadNode.getNext());
}
评论区