20240601 Finished

This commit is contained in:
jackfiled 2024-06-01 11:38:21 +08:00
parent 7f261075d1
commit a62ba27732

View File

@ -0,0 +1,52 @@
// [86] Partition List
using LeetCodeSharp.Utils;
namespace LeetCodeSharp.Problems86
{
// Submission codes start here
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution
{
public ListNode Partition(ListNode head, int x)
{
var dummySmallNode = new ListNode(-1);
var smallNode = dummySmallNode;
var dummyBigNode = new ListNode(-1);
var bigNode = dummyBigNode;
while (head != null)
{
if (head.val < x)
{
smallNode.next = head;
smallNode = smallNode.next;
}
else
{
bigNode.next = head;
bigNode = bigNode.next;
}
head = head.next;
}
bigNode.next = null;
smallNode.next = dummyBigNode.next;
return dummySmallNode.next;
}
}
// Submission codes end here
}