题目链接:https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=13&&tqId=11168&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

  思路:现在我们可以创建一个新链表的头,然后当遍历给出的链表,我们将当前结点按照头插法的规则插入到新的链表中,如此一来,我们要保证我们一直可以遍历给出的链表,此时我们就需要再设置一个之后指向当前结点的下一个结点的指针来保证我们丢失当前的链表。如图:
反转链表

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

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode ReverseList(ListNode head) {
        if(head == null) return null;
        ListNode newList = null, next = head.next;
        while(head != null) {
            head.next = newList;
            newList = head;
            head = next;
            if(head != null) next = head.next;
        }
        return newList;
    }
}