五道經典鍊錶題

2021-09-29 22:01:02 字數 1157 閱讀 4345

/**

* definition for singly-linked list.

* public class listnode

* }*/

public static listnode reversebetween(listnode head, int m, int n) 

// 將鍊錶從m到n的節點反向

for (int i = 0; i < n-m; i++)

return dump.next;

}

public listnode detectcycle(listnode head) 

// 設定快慢指標,每次迴圈,慢指標移動一步,快指標移動兩步

listnode slow1 = head;

listnode fast = head;

while (fast.next != null && fast.next.next != null)

return slow2;}}

return null;

}

public listnode mergetwolists(listnode l1, listnode l2)  else 

cur = cur.next;

}// 拼接剩餘鍊錶

if (node1 == null)

if (node2 == null)

return dummy.next;

}

public listnode removenthfromend(listnode head, int n) 

// 兩個指標同步移動,當快指標到達末尾時,慢指標到達倒數第n個節點

while (fast != null)

slow.next = slow.next.next;

return dummy.next;

}

public listnode middlenode(listnode head) 

// 鍊錶個數為奇數和偶數的處理

if (fast.next == null) else

}

記一道經典前端題

題目如下 function foo return this foo.getname function foo.prototype.getname function var getname function function getname 輸出值 foo.getname getname foo ge...

記一道經典前端題

題目如下 function foo return this foo.getname function foo.prototype.getname function var getname function function getname 輸出值 foo.getname getname foo ge...

五道邏輯思維題

從1到100排列成一排,總共100個數,每一輪抽走奇數字置的數,問最後剩下哪個數?先思考小規模的問題,例如從1到10,模擬整個抽取過程如下。第1輪抽取完剩下所有偶數,如果都除以2,就變成了1到5的小問題 第2輪抽取後,還是剩下偶數,再除以2,變成1到2。所以最後剩下的數如果因式分解,就是包含因數2最...