public class LinkReverse { /** * 带头结点的链表翻转 */ private void reverse1(LinkNode head) { LinkNode p = head.getNext(); LinkNode q = p; head.setNext(null); while (q != null) { q = p.getNext(); p.setNext(head.getNext()); head.setNext(p); p = q; } } /** * 不带头结点的链表翻转 */ private void reverse2(LinkNode head) { if (head == null) { return; } LinkNode p = head.getNext(); LinkNode q; head.setNext(null); while (p != null) { q = p.getNext(); p.setNext(head); head = p; p = q; } } }
原创文章,作者:geekgao,如若转载,请注明出处:https://www.geekgao.cn/archives/2695