反转单向链表

来源:互联网 发布:浙江高考数据网 编辑:程序博客网 时间:2024/06/16 02:29

206. Reverse Linked List

思路:迭代,将链表分为头结点head和后续链表reslist。假设reslist已完成反转,relist的头结点为rehead: rehead->head;head->null;

public ListNode reverseList(ListNode head) {        if (head==null||head.next==null) return head;        ListNode p = reverseList(head.next);        head.next.next = head;        head.next = null;        return p;            }