手撕代码之反转单链表

public class listNode {
    
    
    int data;
    listNode next;
    public listNode(int data, listNode next) {
    
    
        this.data = data;
        this.next = next;
    }
}
public class listNodeReverse {
    
    
    public static void main(String[] args) {
    
    
        listNode D = new listNode(4, null);
        listNode C = new listNode(3, D);
        listNode B = new listNode(2, C);
        listNode A = new listNode(1, B);
        listNode  node= reverse(A);
        System.out.println("null");
    }
    public static listNode reverse(listNode listnode) {
    
    
        //迭代的思想
        listNode pre = null;
        listNode now = listnode;
        while (now != null) {
    
    
            listNode next =now.next;
            now.next=pre;
            pre = now;
            now=next;
        }
        return pre;
    }

}

原文连接https://www.cnblogs.com/xiaonantianmen/p/9806643.html

猜你喜欢

转载自blog.csdn.net/qq_43518425/article/details/114257636