要求进行链表反转
实现:

public class ReverseList {
    public static void main(String[] args) {
        Node head = new Node(1);
        Node node1 = new Node(2);
        Node node2 = new Node(3);
        Node node3 = new Node(4);
        head.next = node1;
        node1.next = node2;
        node2.next = node3;
        Node first = head;
        while(first != null){
            System.out.println(first.getValue());
            first = first.getNext();
        }
        System.out.println("-----------");
        head = reverseList(head);
        while(head != null){
            System.out.println(head.getValue());
            head = head.getNext();
        }
    }
  /**
   * 递归
   * @param head
   * @return
   * 思想:从后往前逆序反转指针域的指向
   */
 public static Node reverse(Node head){
        if(head == null || head.getNext() == null)return head;
        Node newHead = reverse(head.getNext());
        Node temp = head.getNext();
        temp.setNext(head);
        head.setNext(null);
        return newHead;
    }
    /**
     * @param head
     * @return
     * pre:上一节点
     * cur:当前节点
     * tmp:临时节点,用于保存当前节点的下一节点
     * 思想:从前往后反转各个节点的指针域的指向
     * 过程:与交换两变量的值一样,先使用一个临时变量保存当前节点的下一节点,然后交换pre与cur节点
     */
 public static Node reverseList(Node head){
        if(head == null){return head;}
        Node pre = head;
        Node cur = head.getNext();
        Node tmp;
        while(cur != null){
            tmp = cur.getNext();
            cur.setNext(pre);
            pre = cur;
            cur = tmp;
        }
        head.setNext(null);
        return pre;
    }
}
 class Node<E>{
    int value;
    Node next;
    Node(int x){
        value = x;
    }
     public int getValue() {
         return value;
     }
     public void setValue(int value) {
         this.value = value;
     }
     public Node getNext() {
         return next;
     }
     public void setNext(Node next) {
         this.next = next;
     }
 }

这里有一张图说明下就地反转的思想:
image.png
dummy为头节点
prev连接下一次需要反转的节点
pCur指向下一次需要反转的节点
伪代码:

prev.next = pCur.next;
pCur.next = dummy.next;;
dummy.next = pCur;
pCur = prev.next;

一直循环直到pCur指向的节点为null.


步履不停
38 声望13 粉丝

好走的都是下坡路