翻转链表

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
//  temp = c.next  理解为  c.next指向的东西 temp指向的一致
class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode pre =null;
        ListNode c = head;
        ListNode temp = null;
        while(c!=null){

    
            //    保留下一个结点
               temp = c.next;
            //    翻转指针
               c.next = pre;
            //    更新pre
                pre = c  ;
                // 更新c
                c  =temp;

}
        return pre;
    }
}

  

posted @ 2022-01-11 21:18  11111ghm  阅读(17)  评论(0编辑  收藏  举报