输入一个链表,反转链表后,输出新链表的表头。

public class Solution {
        public ListNode ReverseList(ListNode head) {

            ListNode qHead=null;//翻转后的头结点

            if(head==null || head.next == null){
                return head;
            }

            ListNode p = head;


            while (p!=null){
                ListNode nextNode = p.next;//暂存
                p.next = qHead;
                qHead = p;
                p = nextNode;
            }
            return qHead;
        }
    }

猜你喜欢

转载自www.cnblogs.com/q-1993/p/10545904.html