力扣-206.反转链表
题目链接
206.反转链表
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;
}
class Solution {public ListNode reverseList(ListNode head) {ListNode h = new ListNode(0);ListNode p = head;while (p != null) {ListNode tmp = p;p = p.next;tmp.next = h.next;h.next = tmp;}return h.next;}
}
小结:头插法秒了,注意题中是不带头结点的链表。