From 185e19748c7fadc5bade6ef6e775991e395ed7c9 Mon Sep 17 00:00:00 2001 From: vanyongqi <46806467+vanyongqi@users.noreply.github.com> Date: Thu, 20 Oct 2022 19:49:24 +0800 Subject: [PATCH] =?UTF-8?q?Update=200019.=E5=88=A0=E9=99=A4=E9=93=BE?= =?UTF-8?q?=E8=A1=A8=E7=9A=84=E5=80=92=E6=95=B0=E7=AC=ACN=E4=B8=AA?= =?UTF-8?q?=E8=8A=82=E7=82=B9.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加C语言版本,已通过Leetcode验证 --- ...0019.删除链表的倒数第N个节点.md | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/problems/0019.删除链表的倒数第N个节点.md b/problems/0019.删除链表的倒数第N个节点.md index 314c396e..b0641b5f 100644 --- a/problems/0019.删除链表的倒数第N个节点.md +++ b/problems/0019.删除链表的倒数第N个节点.md @@ -364,6 +364,40 @@ impl Solution { dummy_head.next } } +``` +C语言 +```c +/**c语言单链表的定义 + * Definition for singly-linked list. + * struct ListNode { + * int val; + * struct ListNode *next; + * }; + */ +struct ListNode* removeNthFromEnd(struct ListNode* head, int n) { + //定义虚拟头节点dummy 并初始化使其指向head + struct ListNode* dummy = malloc(sizeof(struct ListNode)); + dummy->val = 0; + dummy->next = head; + //定义 fast slow 双指针 + struct ListNode* fast = head; + struct ListNode* slow = dummy; + + for (int i = 0; i < n; ++i) { + fast = fast->next; + } + while (fast) { + fast = fast->next; + slow = slow->next; + } + slow->next = slow->next->next;//删除倒数第n个节点 + head = dummy->next; + free(dummy);//删除虚拟节点dummy + return head; +} + + + ```