1. 程式人生 > >劍指 Offer - 15:反轉連結串列

劍指 Offer - 15:反轉連結串列

題目描述

輸入一個連結串列,反轉連結串列後,輸出新連結串列的表頭

題目連結:https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca

解題思路

思路1:藉助棧

思路2:遞迴

思路3:連結串列原地反轉

public class Solution {
    public ListNode ReverseList(ListNode head) {
        if (head == null || head.next == null) return head;
ListNode pre = null; ListNode next = null; while (head != null) { next = head.next; head.next = pre; pre = head; head = next; } return pre; } }