算法复习之 链表反转

思路:链表反转,我这里采用的是JDK 1.7中HashMap拉链法所采用的头插法。

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/

import java.util.*;

public class Solution {
    public ListNode ReverseList(ListNode head) {
        /*头插法*/
        ListNode next = null;
        ListNode pre = null;
        while(head != null) {
            next = head.next;
            head.next = pre;
            pre = head;
            head = next;
        }
        return pre;
    }
}
posted @ 2021-04-28 20:50  Cruel_King  阅读(33)  评论(0编辑  收藏  举报