[LeetCode]86. Partition List分離鏈表
阿新 • • 發佈:2018-02-08
pos log while pan emp blog ret body 鏈表
/* 這個題是medium的意思應該是用雙指針的方法做,如果使用下邊的新建鏈表的方法,就是easy的題目了 雙指針會用到很多鏈表的相連操作 */ public ListNode partition(ListNode head, int x) { ListNode s = null; ListNode b = null; ListNode temp=null,res=null; while (head!=null) { int cur = head.val;if (head.val<x) { if (s==null) { s = new ListNode(cur); res = s; } else { s.next = new ListNode(cur); s = s.next; } }else { if (b==null) { b = new ListNode(cur); temp = b; } else { b.next = new ListNode(cur); b = b.next; } } head = head.next; }if (s==null) return temp; s.next = temp; return res; }
[LeetCode]86. Partition List分離鏈表