数据结构-单链表的逆转

单链表的逆转是指除了头结点外,要把开始的节点的指向逆转一下。

https://blog.csdn.net/king8611/article/details/81099344这是刚才写的单链表,没有反转函数。
因为单纯反转会出现节点损失,所以这里需要用到中间节点储存一下,最后头结点再指向尾节点就行了。

	public void reveser(){							//单链表的反转
		Node<T> p=this.head.next,temp=p,last=null;	//p是遍历节点,temp用来中间转换,last用来储存上一个节点
		while(p!=null){
			temp=p;									//先把temp节点变为p,
			p=temp.next;							//然后遍历节点等于temp
			temp.next=last;							//temp节点指向上一个节点
			last=temp;								//上一个节点变为中间节点
		}
		this.head.next=temp;
	}

测试函数发现过了嗯~ o(* ̄▽ ̄*)o。

猜你喜欢

转载自blog.csdn.net/king8611/article/details/81101505