[LeetCode]86. Partition List分离链表

/*
    这个题是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;
    }

原文地址:https://www.cnblogs.com/stAr-1/p/8431234.html

时间: 2024-11-09 08:57:16

[LeetCode]86. Partition List分离链表的相关文章

[leetcode]86. Partition List划分链表

Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. Input: head = 1->4->3->2->

[LeetCode] 86. Partition List 划分链表

Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. Example: Input: head = 1->4->3-

leetCode 86.Partition List(分区链表) 解题思路和方法

Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. For example, Given 1->4->3->

LeetCode --- 86. Partition List

题目链接:Partition List Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. For example,

[LeetCode] 86. Partition List Java

题目: Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. For example,Given 1->4->3-&

19.2.23 [LeetCode 86] Partition List

Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. Example: Input: head = 1->4->3-

[LeetCode] 86. Partition List 解题思路

Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. For example,Given 1->4->3->2

[LeetCode] 86. Partition List_Medium tag: Linked List

Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. Example: Input: head = 1->4->3-

[LeetCode 题解]: Partition List

Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. For example,Given 1->4->3->2