78.逆リンクリスト

タイトル説明

リンクリストを入力し、リンクリストを反転した後、新しいリンクリストのヘッダーを出力します。

例1

入る

{1,2,3}

戻り値

{3,2,1}
コードの実装
/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode ReverseList(ListNode head) {
        ListNode pre = null;
        ListNode cur = head;
        while (cur != null) {
            ListNode tmp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = tmp;
        }
        return pre;
    }
}

 

おすすめ

転載: blog.csdn.net/xiao__jia__jia/article/details/113478905