86.分隔連結串列
給定一個連結串列和一個特定值 x,對連結串列進行分隔,使得所有小於 x 的節點都在大於或等於 x 的節點之前。
你應當保留兩個分割槽中每個節點的初始相對位置。
示例:
輸入: head = 1->4->3->2->5->2, x = 3
輸出: 1->2->2->4->3->5
連結:https://leetcode-cn.com/problems/partition-list
思路:整兩個全新的節點,一個存放比x小的,一個存放比x大的,然後小的尾巴連上大的頭。
class Solution { public ListNode partition(ListNode head, intx) { if (head == null || head.next == null) { return head;//注意這邊是return head } ListNode temp=head; ListNode Head_big=new ListNode(-1); ListNode Head_big_ptr=Head_big; ListNode Head_small=new ListNode(-1); ListNode Head_small_ptr=Head_small;while (temp!=null) { if(temp.val<x) { Head_small_ptr.next=temp; // System.out.println(temp.val); Head_small_ptr=Head_small_ptr.next; } else { Head_big_ptr.next=temp; Head_big_ptr=Head_big_ptr.next; } temp=temp.next; } Head_small_ptr.next=Head_big.next; Head_big_ptr.next=null; ListNode kk=Head_small.next; return kk; } }
官方題解:
雙指標法:
直覺
我們可以用兩個指標before 和 after 來追蹤上述的兩個連結串列。兩個指標可以用於分別建立兩個連結串列,然後將這兩個連結串列連線即可獲得所需的連結串列。
演算法
初始化兩個指標 before 和 after。在實現中,我們將兩個指標初始化為啞 ListNode。這有助於減少條件判斷。(不信的話,你可以試著寫一個不帶啞結點的方法自己看看!)
利用head指標遍歷原連結串列。
若head 指標指向的元素值 小於 x,該節點應當是 before 連結串列的一部分。因此我們將其移到 before 中。
否則,該節點應當是after 連結串列的一部分。因此我們將其移到 after 中。
遍歷完原有連結串列的全部元素之後,我們得到了兩個連結串列 before 和 after。原有連結串列的元素或者在before 中或者在 after 中,這取決於它們的值。
*`注意:` 由於我們從左到右遍歷了原有連結串列,故兩個連結串列中元素的相對順序不會發生變化。另外值得注意的是,在圖中我們完好地保留了原有連結串列。事實上,在演算法實現中,我們將節點從原有連結串列中移除,並將它們新增到別的連結串列中。我們沒有使用任何額外的空間,只是將原有的連結串列元素進行移動。*
現在,可以將 before 和 after 連線,組成所求的連結串列。
為了演算法實現更容易,我們使用了啞結點初始化。不能讓啞結點成為返回連結串列中的一部分,因此在組合兩個連結串列時需要向前移動一個節點。
class Solution { public ListNode partition(ListNode head, int x) { // before and after are the two pointers used to create the two list // before_head and after_head are used to save the heads of the two lists. // All of these are initialized with the dummy nodes created. ListNode before_head = new ListNode(0); ListNode before = before_head; ListNode after_head = new ListNode(0); ListNode after = after_head; while (head != null) { // If the original list node is lesser than the given x, // assign it to the before list. if (head.val < x) { before.next = head; before = before.next; } else { // If the original list node is greater or equal to the given x, // assign it to the after list. after.next = head; after = after.next; } // move ahead in the original list head = head.next; } // Last node of "after" list would also be ending node of the reformed list after.next = null; // Once all the nodes are correctly assigned to the two lists, // combine them to form a single list which would be returned. before.next = after_head.next; return before_head.next; } }