【Lintcode】35。逆にリンクされたリスト

タイトルアドレス:

https://www.lintcode.com/problem/reverse-linked-list/description

リンクされたリストを反転します。ヘッド補間でノードを転送する場合は、次のノードのバッファリングに注意してください。コードは次のとおりです。

public class Solution {
    /**
     * @param head: n
     * @return: The new head of reversed linked list.
     */
    public ListNode reverse(ListNode head) {
        // write your code here
        ListNode res = null;
        while (head != null) {
            ListNode tmp = head.next;
            head.next = res;
            res = head;
            head = tmp;
        }
        
        return res;
    }
}

class ListNode {
	int val;
	ListNode next;
	ListNode(int x) {
		val = x;
	}
}

時間の複雑さ O(n) 、スペース 1 O(1)

公開された387元の記事 ウォンの賞賛0 ビュー10000 +

おすすめ

転載: blog.csdn.net/qq_46105170/article/details/105468186