使用Java将链表从 a->b->c 的状态转移为 c->b>a 并且返回新链表的头结点
代码实现:
public static class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
public ListNode reverseV(ListNode head){
if(head == null || head.next == null)
return head;
//声明尾结点并指向空 头结点刷新
ListNode tail = head;
head = head.next;
tail.next = null;
while(head != null){
ListNode tmp = head.next;
head.next = tail;
tail = head;
head = tmp;
}
return tail;
}