05、分隔链表

厨子大约 3 分钟数据结构算法算法基地面试刷题

题目描述

分隔链表open in new window

给你一个链表的头节点 head 和一个特定值 x ,请你对链表进行分隔,使得所有 小于 x 的节点都出现在 大于或等于 x 的节点之前。

你应当 保留 两个分区中每个节点的初始相对位置。

示例 1:

输入:head = [1,4,3,2,5,2], x = 3 输出:[1,2,2,4,3,5] 示例 2:

输入:head = [2,1], x = 2 输出:[1,2]

来源:力扣(LeetCode)

题目解析

这个题目我的做题思路是这样的,我们先创建一个侦察兵,侦察兵负责比较链表值和 x 值,如果 >= 的话则接在 big 链表上,小于则接到 small 链表上,最后一个细节就是我们的 big 链表尾部要加上 null,不然会形成环。这是这个题目的一个小细节,很重要。

中心思想就是,将链表先分后合。

下面我们来看模拟视频吧。希望能给各位带来一丢丢帮助。

代码

Java Code:

class Solution {
    public ListNode partition(ListNode head, int x) {
        ListNode pro = head;
        ListNode big = new ListNode(-1);
        ListNode small = new ListNode(-1);
        ListNode headbig = big;
        ListNode headsmall = small;
        //分
        while (pro != null) {
            //大于时,放到 big 链表上
            if (pro.val >= x) {
                big.next = pro;
                big = big.next;
            //小于时,放到 small 链表上
            }else {
                small.next = pro;
                small = small.next;
            }
            pro = pro.next;
        }
        //细节
        big.next = null;
        //合
        small.next = headbig.next;
        return headsmall.next;
    }
}

C++ Code:

class Solution {
public:
    ListNode* partition(ListNode* head, int x) {
        ListNode * pro = head;
        ListNode * big = new ListNode(-1);
        ListNode * small = new ListNode(-1);
        ListNode * headbig = big;
        ListNode * headsmall = small;
        //分
        while (pro != nullptr) {
            //大于时,放到 big 链表上
            if (pro->val >= x) {
                big->next = pro;
                big = big->next;
            //小于时,放到 small 链表上
            }else {
                small->next = pro;
                small = small->next;
            }
            pro = pro->next;
        }
        //细节
        big->next = nullptr;
        //合
        small->next = headbig->next;
        return headsmall->next;
    }
};

JS Code:

var partition = function (head, x) {
  let pro = head;
  let big = new ListNode(-1);
  let small = new ListNode(-1);
  let headbig = big;
  let headsmall = small;
  //分
  while (pro) {
    //大于时,放到 big 链表上
    if (pro.val >= x) {
      big.next = pro;
      big = big.next;
      //小于时,放到 small 链表上
    } else {
      small.next = pro;
      small = small.next;
    }
    pro = pro.next;
  }
  //细节
  big.next = null;
  //合
  small.next = headbig.next;
  return headsmall.next;
};

Python Code:

class Solution:
    def partition(self, head: ListNode, x: int) -> ListNode:
        pro = head
        big = ListNode(-1)
        small = ListNode(-1)
        headbig = big
        headsmall = small
        # 分
        while pro is not None:
            # 大于时,放到 big 链表上
            if pro.val >= x:
                big.next = pro
                big = big.next
            # 小于时,放到 small 链表上
            else:
                small.next = pro
                small = small.next
            pro = pro.next
        # 细节
        big.next = None
        # 合
        small.next = headbig.next
        return headsmall.next

Swift Code:

class Solution {
    func partition(_ head: ListNode?, _ x: Int) -> ListNode? {
        var pro = head
        var big = ListNode(-1)
        var small = ListNode(-1)
        var headbig = big
        var headsmall = small
        //分
        while pro != nil {
            //大于时,放到 big 链表上
            if pro!.val >= x {
                big.next = pro
                big = big.next!
            //小于时,放到 small 链表上
            } else {
                small.next = pro
                small = small.next!
            }
            pro = pro?.next
        }
        //细节
        big.next = nil
        //合
        small.next = headbig.next
        return headsmall.next
    }
}

Go Code:

func partition(head *ListNode, x int) *ListNode {
    big, small := &ListNode{}, &ListNode{}
    headBig, headSmall := big, small
    temp := head
    for temp != nil {
        // 分开存
        if temp.Val < x {
            small.Next = temp
            small = small.Next
        } else {
            big.Next = temp
            big = big.Next
        }
        temp = temp.Next
    }
    // 最后一个节点指向nil
    big.Next = nil
    // 存小数的链表和存大数的连起来
    small.Next = headBig.Next
    return headSmall.Next
}