---
# System prepended metadata

title: 0086. Partition List
tags: [Leetcode, Linked List, Medium]

---

# 0086. Partition List
###### tags: `Leetcode` `Medium` `Linked List`
Link: https://leetcode.com/problems/partition-list/
## 思路
1. 创建两个指针头p, q 分别是 node val大于x的nodes 和 node val小于x的nodes 的dummy start
2. 在遍历原链表的过程中，将节点根据val分别归到p或q的链表中。
## Code 
```java=
class Solution {
    public ListNode partition(ListNode head, int x) {
        ListNode dummy1 = new ListNode(0);
        ListNode dummy2 = new ListNode(0);
        ListNode p = dummy1; 
        ListNode q = dummy2;
        
        while(head!=null){
            if(head.val<x){
                p.next = head;
                p = p.next;
            }
            else{
                q.next = head;
                q = q.next;
            }
            head = head.next;
        }
        
        p.next = dummy2.next;
        q.next = null;
        return dummy1.next;
    }
}
```