タイトルアドレス:
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;
}
}
時間の複雑さ 、スペース 。