public class ReverseSingleList{/*** 递归,在反转当前节点之前先反转后续节点** @param head* @return*/public static Node reverse(Node head){if (null == head || null == head.getNextNode()){return head;...
public class ReverseSingleList
{
* 递归,在反转当前节点之前先反转后续节点
* @param head
* @return
public static Node reverse(Node head)
{
if (null == head || null == head.getNextNode())
{
return head;
}
Node reversedHead = reverse(head.getNextNode());
head.getNextNode().setNextNode(head);
head.setNextNode(null);
return reversedHead;
}
* 遍历,将当前节点的下一个节点缓存后更改当前节点指针
* @param head
* @param mode
* 仅用作区分于同名方法
* @return
public static Node reverse(Node head, int mode)
{
if (null == head)
{
return head;
}
Node pre = head;
Node cur = head.getNextNode();
Node next;
while (null != cur)
{
next = cur.getNextNode();
cur.setNextNode(pre);
pre = cur;
cur = next;
}
// 将原链表的头节点的下一个节点置为null,再将反转后的头节点赋给head
head.setNextNode(null);