feat: 2025-10-27打卡
This commit is contained in:
43
86.partition-list.java
Normal file
43
86.partition-list.java
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* @lc app=leetcode id=86 lang=java
|
||||
*
|
||||
* [86] Partition List
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode() {}
|
||||
* ListNode(int val) { this.val = val; }
|
||||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public ListNode partition(ListNode head, int x) {
|
||||
ListNode tf = new ListNode(0),
|
||||
tb = new ListNode(0);
|
||||
tf.next = null;
|
||||
tb.next = null;
|
||||
ListNode cur = head, tailf = tf, tailb = tb;
|
||||
while(cur != null) {
|
||||
if(cur.val < x) {
|
||||
tailf.next = cur;
|
||||
cur = cur.next;
|
||||
tailf = tailf.next;
|
||||
tailf.next = null;
|
||||
} else {
|
||||
tailb.next = cur;
|
||||
cur = cur.next;
|
||||
tailb = tailb.next;
|
||||
tailb.next = null;
|
||||
}
|
||||
}
|
||||
tailf.next = tb.next;
|
||||
return tf.next;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
Reference in New Issue
Block a user