From a62ba277321476fcc6376be0042e16704c2ca375 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Sat, 1 Jun 2024 11:38:21 +0800 Subject: [PATCH] 20240601 Finished --- LeetCodeSharp/Problems/Solution86.cs | 52 ++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 LeetCodeSharp/Problems/Solution86.cs diff --git a/LeetCodeSharp/Problems/Solution86.cs b/LeetCodeSharp/Problems/Solution86.cs new file mode 100644 index 0000000..1613992 --- /dev/null +++ b/LeetCodeSharp/Problems/Solution86.cs @@ -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 +} \ No newline at end of file