赞
踩
使用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;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。