JavaLeetCode刷题记录Java数据结构和算法

LeetCode 86. 分隔链表

2019-07-09  本文已影响0人  TheKey_

86. 分隔链表

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

示例1:
输入: head = 1->4->3->2->5->2, x = 3
输出: 1->2->2->4->3->5

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/partition-list/
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。


思路:(使用指针)

  1. 将当前节点值小于 x 的放入一个链表中, 大于 x 的放入一个链表中
  2. 将大于 x 的链表拼接到小于 x 的链表之后即可
image.png
public static class ListNode {

        private int val;
        private ListNode next;

        public ListNode(int val) {
            this.val = val;
        }

        //用于测试用例
        public ListNode(int[] arr) {
            if (arr == null || arr.length == 0) throw new NullPointerException("array is Empty");
            this.val = arr[0];
            ListNode cur = this;
            for (int i = 1; i < arr.length; i++) {
                cur.next = new ListNode(arr[i]);
                cur = cur.next;
            }
        }

        @Override
        public String toString() {
            StringBuilder res = new StringBuilder();
            ListNode cur = this;
            while (cur != null) {
                res.append(cur.val + "->");
                cur = cur.next;
            }
            res.append("NULL");
            return res.toString();
        }

    }

   public static ListNode partition(ListNode head, int x) {
        if (head == null) return head;
        ListNode dummyHead = new ListNode(0);
        dummyHead.next = head;
        ListNode cur = head;
        ListNode prev = dummyHead;
        ListNode node = new ListNode(0);
        ListNode p = node;
        while (cur != null) {
            if (cur.val >= x) {
                prev.next = cur.next;
                p.next = new ListNode(cur.val);
                p = p.next;
            } else {
                prev = prev.next;
            }
            cur = cur.next;
        }
        prev.next = node.next;
        return dummyHead.next;
    }

复杂度分析:

思路:
同上,只是不在多余创建节点

 public static ListNode partition(ListNode head, int x) {
        if (head == null) return head;
        ListNode dummyHead = new ListNode(0);
        dummyHead.next = head;
        ListNode cur = head;
        ListNode prev = dummyHead;
        ListNode node = new ListNode(0);
        ListNode p = node;
        while (cur != null) {
            if (cur.val >= x) {
                prev.next = cur.next;
                p.next = cur;
                p = p.next;
            } else {
                prev = prev.next;
            }
            cur = cur.next;
        }
        p.next = null;
        prev.next = node.next;
        return dummyHead.next;
    }

复杂度分析:

思路:利用队列的先进先出(FIFO)特性

  1. 将原链表中节点大于 x 的压入队列 queue1, 大于的压入 queue2
  2. 创建一个新的链表,依次出队然后拼接到新链表即可
 public static ListNode partition(ListNode head, int x) {
        if (head == null) return head;
        ListNode dummyHead = new ListNode(0);
        dummyHead.next = head;
        ListNode cur = head;
        ListNode prev = dummyHead;
        ListNode node = new ListNode(0);
        ListNode p = node;
        while (cur != null) {
            if (cur.val >= x) {
                prev.next = cur.next;
                p.next = cur;
                p = p.next;
            } else {
                prev = prev.next;
            }
            cur = cur.next;
        }
        p.next = null;
        prev.next = node.next;
        return dummyHead.next;
    }

复杂度分析:

public static void main(String[] args) {
         int[] arr = new int[] {1, 4, 3, 2, 5, 2};
         ListNode listNode = new ListNode(arr);
         System.out.println(listNode);
         System.out.println("分割链表:" + partition(listNode, 3));
    }
1->4->3->2->5->2->NULL
分割链表:1->2->2->4->3->5->NULL

上一篇下一篇

猜你喜欢

热点阅读