https://blog.csdn.net/autumn20080101/article/details/7607148
package com.jd.etms.vrs.lean.link; /** * Created by zhangwenjun10 on 2018/11/26. */ public class TestLink { public static void main(String args[]) { LinkNode headNode = creatSingleLink(); printSingleLink(headNode); LinkNode temNode = reverseSingleLink(headNode); System.out.println(); printSingleLink(temNode); LinkNode reverseNode = reverseLink(temNode); System.out.println(); printSingleLink(reverseNode); } public static LinkNode creatSingleLink() { LinkNode headNode = new LinkNode(0); headNode.next = new LinkNode(1); headNode.next.next = new LinkNode(2); headNode.next.next.next = new LinkNode(3); headNode.next.next.next.next = new LinkNode(4); headNode.next.next.next.next.next = new LinkNode(5); return headNode; } public static void printSingleLink(LinkNode headNode) { if (headNode != null) { System.out.print("" + headNode.value); } if (null != headNode.next) { printSingleLink(headNode.next); } } /*单链表逆序*/ public static LinkNode reverseSingleLink(LinkNode headNode) { if (null == headNode || null == headNode.next) { return headNode; } else { LinkNode temNode = headNode.next; LinkNode newHead = reverseSingleLink(temNode); headNode.next.next = headNode; headNode.next = null; return newHead; } } public static LinkNode reverseLink(LinkNode head){ if(head == null || head.next == null){ return head; } LinkNode prev = null; LinkNode next = null; while (head != null){ next = head.next; head.next = prev; prev = head; head = next; } return prev; } }