先一次遍歷找到這兩個元素curr
然後大換血
public static Link SwitchPoints(Link head
{
if (p == head || q == head)
throw new Exception(
if (p == q)
return head;
//find p and q in the link
Link curr = head;
Link curr
Link curr
Link pre
Link pre
int count =
while (curr != null)
{
if (curr
{
pre
count++;
if (count ==
break;
}
else if (curr
{
pre
count++;
if (count ==
break;
}
curr = curr
}
curr = curr
pre
curr
pre
curr
return head;
}
注意特例
From:http://tw.wingwit.com/Article/program/sjjg/201404/30588.html