循环
思路:
从头节点按照顺序切割链表组成新的反转链表。
反转的意思将链表从头到尾,切割并反转指针域。
示意图:
结果图:
代码:
//单链表反转1,循环反转
//链表需要节点的相关节点如下:
//前驱节点,predecessorNode
//当前节点,currentNode
//后续节点,nextCode
func (this *LinkedList) ReverseCycle(head *ListNode) {
if head==nil ||head.next==nil{
return
}
predecessorNode:=NewListNode(nil)//前驱节点
nextCode:=NewListNode(nil)//后续节点
currentNode:=head//当前节点
for currentNode!=nil{
nextCode=currentNode.next//保存后续节点,防止节点丢失
currentNode.next=predecessorNode//断链
predecessorNode=currentNode//前进1,当前节点移至下一个节点
currentNode=nextCode//前进2
}
this.head=predecessorNode//将链表头节点指向前节点
}
引用博客:
https://mp.weixin.qq.com/s/qadX-WqLqt7Q0IvKY8V9Ow