public void ReverseBetween(int m, int n)
{
if (this.Head == null)
{
return;
}
LinkedListNode dummy = new LinkedListNode(-1, this.Head);
LinkedListNode prevStart = null, prev = dummy;
for (int i = 0; i < m; ++i)
{
prevStart = prev;
prev = prev.Next;
}
LinkedListNode revStart = prev;
LinkedListNode curr = prev.Next;
for (int i = 0; i < n - m; ++i)
{
LinkedListNode next = curr.Next;
curr.Next = prev;
prev = curr;
curr = next;
}
prevStart.Next = prev;
revStart.Next = curr;
this.Head = dummy.Next;
}
No comments:
Post a Comment