Skip to content

Commit

Permalink
feat: update leetcode solutions: No.0019. Remove Nth Node From End of…
Browse files Browse the repository at this point in the history
… List
  • Loading branch information
yanglbme committed Mar 28, 2021
1 parent f69c63a commit 692acff
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 45 deletions.
68 changes: 64 additions & 4 deletions solution/0000-0099/0019.Remove Nth Node From End of List/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

<!-- 这里可写通用的实现逻辑 -->

利用快慢指针。

<!-- tabs:start -->

### **Python3**
Expand All @@ -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
```
Expand Down Expand Up @@ -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
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,12 @@ After removing the second node from the end, the linked list becomes <strong>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
```
Expand Down Expand Up @@ -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
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
return dummy.next

0 comments on commit 692acff

Please sign in to comment.