diff --git a/solution/0000-0099/0019.Remove Nth Node From End of List/README.md b/solution/0000-0099/0019.Remove Nth Node From End of List/README.md index 321f5af9ccaff..a635b8934d4e4 100644 --- a/solution/0000-0099/0019.Remove Nth Node From End of List/README.md +++ b/solution/0000-0099/0019.Remove Nth Node From End of List/README.md @@ -26,6 +26,8 @@ +利用快慢指针。 + ### **Python3** @@ -40,13 +42,12 @@ # self.next = next class Solution: def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode: - dummy = ListNode(0, head) + dummy = ListNode(next=head) p = q = dummy for i in range(n): p = p.next - while p.next is not None: - p = p.next - q = q.next + while p.next: + p, q = p.next, q.next q.next = q.next.next return dummy.next ``` @@ -83,6 +84,65 @@ class Solution { } ``` +### **C++** + +```cpp +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* removeNthFromEnd(ListNode* head, int n) { + ListNode* dummy = new ListNode(0, head); + ListNode* p = dummy; + ListNode* q = dummy; + while (n-- > 0) { + p = p->next; + } + while (p->next != nullptr) { + p = p->next; + q = q->next; + } + q->next = q->next->next; + return dummy->next; + } +}; +``` + +### **Go** + +```go +/** + * Definition for singly-linked list. + * type ListNode struct { + * Val int + * Next *ListNode + * } + */ +func removeNthFromEnd(head *ListNode, n int) *ListNode { + dummy := &ListNode{Val:0, Next:head} + p := dummy + q := dummy + for n > 0 { + p = p.Next + n-- + } + for p.Next != nil { + p = p.Next + q = q.Next + } + q.Next = q.Next.Next + return dummy.Next +} +``` + ### **...** ``` diff --git a/solution/0000-0099/0019.Remove Nth Node From End of List/README_EN.md b/solution/0000-0099/0019.Remove Nth Node From End of List/README_EN.md index 55f0a9cb5a46f..6884bf5b65fa5 100644 --- a/solution/0000-0099/0019.Remove Nth Node From End of List/README_EN.md +++ b/solution/0000-0099/0019.Remove Nth Node From End of List/README_EN.md @@ -40,13 +40,12 @@ After removing the second node from the end, the linked list becomes 1-& # self.next = next class Solution: def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode: - dummy = ListNode(0, head) + dummy = ListNode(next=head) p = q = dummy for i in range(n): p = p.next - while p.next is not None: - p = p.next - q = q.next + while p.next: + p, q = p.next, q.next q.next = q.next.next return dummy.next ``` @@ -81,6 +80,65 @@ class Solution { } ``` +### **C++** + +```cpp +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* removeNthFromEnd(ListNode* head, int n) { + ListNode* dummy = new ListNode(0, head); + ListNode* p = dummy; + ListNode* q = dummy; + while (n-- > 0) { + p = p->next; + } + while (p->next != nullptr) { + p = p->next; + q = q->next; + } + q->next = q->next->next; + return dummy->next; + } +}; +``` + +### **Go** + +```go +/** + * Definition for singly-linked list. + * type ListNode struct { + * Val int + * Next *ListNode + * } + */ +func removeNthFromEnd(head *ListNode, n int) *ListNode { + dummy := &ListNode{Val:0, Next:head} + p := dummy + q := dummy + for n > 0 { + p = p.Next + n-- + } + for p.Next != nil { + p = p.Next + q = q.Next + } + q.Next = q.Next.Next + return dummy.Next +} +``` + ### **...** ``` diff --git a/solution/0000-0099/0019.Remove Nth Node From End of List/Solution.cpp b/solution/0000-0099/0019.Remove Nth Node From End of List/Solution.cpp index d9e9248e9f2f7..1dce3eaf2bf33 100644 --- a/solution/0000-0099/0019.Remove Nth Node From End of List/Solution.cpp +++ b/solution/0000-0099/0019.Remove Nth Node From End of List/Solution.cpp @@ -1,25 +1,27 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ class Solution { public: ListNode* removeNthFromEnd(ListNode* head, int n) { - ListNode *p, *q ; - q = head ; - p = head ; - - for (int i = 0; i < n; ++i) - q = q->next ; - - if (!q) - { - return head->next ; + ListNode* dummy = new ListNode(0, head); + ListNode* p = dummy; + ListNode* q = dummy; + while (n-- > 0) { + p = p->next; } - - while (q->next) - { - q = q->next ; - p = p->next ; + while (p->next != nullptr) { + p = p->next; + q = q->next; } - - p->next = p->next->next ; - return head ; + q->next = q->next->next; + return dummy->next; } }; \ No newline at end of file diff --git a/solution/0000-0099/0019.Remove Nth Node From End of List/Solution.go b/solution/0000-0099/0019.Remove Nth Node From End of List/Solution.go index 1f754b39955e7..928d4ef88e56d 100644 --- a/solution/0000-0099/0019.Remove Nth Node From End of List/Solution.go +++ b/solution/0000-0099/0019.Remove Nth Node From End of List/Solution.go @@ -6,18 +6,17 @@ * } */ func removeNthFromEnd(head *ListNode, n int) *ListNode { - node0 := &ListNode{Val:0} - node0.Next = head - left := node0 - right := node0 - for n>0 { - right = right.Next - n-- - } - for right.Next != nil { - left = left.Next - right = right.Next - } - left.Next = left.Next.Next - return node0.Next + dummy := &ListNode{Val:0, Next:head} + p := dummy + q := dummy + for n > 0 { + p = p.Next + n-- + } + for p.Next != nil { + p = p.Next + q = q.Next + } + q.Next = q.Next.Next + return dummy.Next } \ No newline at end of file diff --git a/solution/0000-0099/0019.Remove Nth Node From End of List/Solution.py b/solution/0000-0099/0019.Remove Nth Node From End of List/Solution.py index 9666d7ab3603b..48ef376a38ddf 100644 --- a/solution/0000-0099/0019.Remove Nth Node From End of List/Solution.py +++ b/solution/0000-0099/0019.Remove Nth Node From End of List/Solution.py @@ -5,12 +5,11 @@ # self.next = next class Solution: def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode: - dummy = ListNode(0, head) + dummy = ListNode(next=head) p = q = dummy for i in range(n): p = p.next - while p.next is not None: - p = p.next - q = q.next + while p.next: + p, q = p.next, q.next q.next = q.next.next - return dummy.next \ No newline at end of file + return dummy.next