使用Java实现反转链表源码
时间: 2023-03-16 12:44:17 浏览: 121
可以使用以下Java代码实现反转链表: public class ReverseLinkedList {
static class Node {
int val;
Node next;
Node(int val) {
this.val = val;
}
}
public static Node reverseLinkedList(Node head) {
Node prev = null;
Node current = head;
while(current != null) {
Node next = current.next;
current.next = prev;
prev = current;
current = next;
}
return prev;
}
}
阅读全文