From 6316fff080e11124c6d56d45f931207cb0bfd9d4 Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Tue, 7 Nov 2023 09:13:15 +0800 Subject: [PATCH] =?UTF-8?q?Update=20707.=E8=AE=BE=E8=AE=A1=E9=93=BE?= =?UTF-8?q?=E8=A1=A8=EF=BC=8C=E6=B7=BB=E5=8A=A0C#=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0707.设计链表.md | 71 +++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/problems/0707.设计链表.md b/problems/0707.设计链表.md index c27f0107..a08227d9 100644 --- a/problems/0707.设计链表.md +++ b/problems/0707.设计链表.md @@ -1485,6 +1485,77 @@ impl MyLinkedList { } ``` +### C# +```C# +class ListNode +{ + public int val; + public ListNode next; + public ListNode(int val) { this.val = val; } +} +public class MyLinkedList +{ + ListNode dummyHead; + int count; + + public MyLinkedList() + { + dummyHead = new ListNode(0); + count = 0; + } + + public int Get(int index) + { + if (index < 0 || count <= index) return -1; + ListNode current = dummyHead; + for (int i = 0; i <= index; i++) + { + current = current.next; + } + return current.val; + } + + public void AddAtHead(int val) + { + AddAtIndex(0, val); + } + + public void AddAtTail(int val) + { + AddAtIndex(count, val); + } + + public void AddAtIndex(int index, int val) + { + if (index > count) return; + index = Math.Max(0, index); + count++; + ListNode tmp1 = dummyHead; + for (int i = 0; i < index; i++) + { + tmp1 = tmp1.next; + } + ListNode tmp2 = new ListNode(val); + tmp2.next = tmp1.next; + tmp1.next = tmp2; + } + + public void DeleteAtIndex(int index) + { + + if (index >= count || index < 0) return; + var tmp1 = dummyHead; + for (int i = 0; i < index; i++) + { + tmp1 = tmp1.next; + } + tmp1.next = tmp1.next.next; + count--; + + } +} +``` +