From 54e3ac8c5a20c3ede29f19bea7c8de5a08aeb586 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sat, 23 Mar 2024 21:38:06 +0100 Subject: [PATCH] =?UTF-8?q?cs:=20add=20=C2=AB143.=20Reorder=20List=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- cs/reorder-list.cs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 cs/reorder-list.cs diff --git a/cs/reorder-list.cs b/cs/reorder-list.cs new file mode 100644 index 0000000..83af4e4 --- /dev/null +++ b/cs/reorder-list.cs @@ -0,0 +1,38 @@ +using System.Collections.Generic; + +/** + * 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 { + private List ListAsList(ListNode head) { + var result = new List(); + + for (var node = head; node != null; node = node.next) { + result.Add(node); + } + + return result; + } + + public void ReorderList(ListNode head) { + var nodes = ListAsList(head); + + ListNode node = head; + for (int i = 1, j = nodes.Count - 1; i <= j; ++i, --j) { + node.next = nodes[j]; + node = node.next; + + node.next = nodes[i]; + node = node.next; + } + node.next = null; + } +}