今天去际链面试,结果在coding这一关又折了。
那就总结一下吧,就当再手撕一下代码
首先定义一个listNode:
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
时间: 2024-09-30 07:44:29