147. Insertion Sort List

Sort a linked list using insertion sort.
Algorithm of Insertion Sort:
1、Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list.
2、At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there.
3、It repeats until no input elements remain.

這道題,我看了插入排序,插入排序需要與前面的值做比較,但題目給的ListNode是單鏈表,所以我不太知道怎麼獲取一個節點的前一個節點。看了Discuss,有人新生成了一個開始節點,迭代給出的單鏈表,插入這個新生成的開始節點的後面。。。
慢慢努力吧。最起碼先要把插入排序的代碼熟練掌握吧,不要看見了就百度!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章