java算法题---链表反转

链表反转主要的思路是交换前后两个指针

  private static Node reverseListNode(Node head) {
        if (head == null || head.getNext() == null) {
            return head;
        }
        Node pre = null;
        Node cur = head;
        Node next = null;

        while (cur != null) {
            next = cur.getNext();
            cur.setNext(pre);
            pre = cur;
            cur = next;
        }
        return pre;
    }

你可能感兴趣的:(java算法题---链表反转)