链表

    科技2022-07-15  108

    1、反转一个链表 206

    class Solution { public ListNode reverseList(ListNode head) { ListNode pre = null; ListNode cur = head; while (cur != null){ ListNode next = cur.next; cur.next = pre; pre =cur; cur = next; } return pre; } }

    递归实现:

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

     

    Processed: 0.018, SQL: 8