[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->2->5->2 and x = 3,
return 1->2->2->4->3->5.

题意及分析:给出一个链表和一个值x,将链表中值小于x的点移动到值大于等于x的点之前,分别需要保持保持两部分中点的相对位置不变。可以分别得到大于等于x的链表和小于x的链表,然后把两个链表组合起来就是需要的结果。

代码:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode partition(ListNode head, int x) {

        ListNode list1=new ListNode(0);
        ListNode list2=new ListNode(0);
        ListNode p1=list1;
        ListNode p2=list2;

        ListNode start=head;
        while(start!=null){
            int value=start.val;
            if(value<x){
                p1.next = start;
                p1=p1.next;
                // list1.next=null;
            }else{
                p2.next = start;
                p2=p2.next;
            }
            start=start.next;
        }
        p2.next=null;

        p1.next=list2.next;

        return list1.next;
    }
}
时间: 2024-09-29 21:05:15

[LeetCode] 86. Partition List Java的相关文章

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 划分链表

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

86. Partition List java solutions

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(分区链表) 解题思路和方法

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分离链表

/* 这个题是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) { i

[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->

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_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-