天天看點

[LeetCode] Reverse Linked List II解題思路實作代碼

reverse a linked list from position m to n. do it in-place and in one-pass.

for example:

given <code>1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;null</code>, m = 2 and n = 4,

return <code>1-&gt;4-&gt;3-&gt;2-&gt;5-&gt;null</code>.

note:

given m, n satisfy the following condition:

1 ≤ m ≤ n ≤ length of list.

是以,隻需将m到n部分翻轉,然後将m的字首beforem指向該部分反轉後的頭結點即可。如果beforem為null,則m=1,m到n部分的頭結點即為連結清單的頭結點。