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

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;
        }
    }

 

posted @ 2019-03-17 10:52  紫色的雪  阅读(992)  评论(0)    收藏  举报