Skip to content

Commit 3d15735

Browse files
committed
019_Remove_Nth_Node_From_End_of_List
1 parent a165f91 commit 3d15735

3 files changed

+76
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Also, there are open source implementations for basic data structs and algorithm
2323
| 15 | [3Sum](https://leetcode.com/problems/3sum/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/015_3Sum.py) | 1. Sort and O(n^2) search with three points <br>2. Multiple Two Sum (Problem 1) |
2424
| 16 | [3Sum Closest](https://leetcode.com/problems/3sum-closest/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/016_3Sum_Closest.py) | Sort and Multiple Two Sum check abs|
2525
| 18 | [4Sum](https://leetcode.com/problems/4sum/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/018_4Sum.py) | The same as 3Sum, but we can merge pairs with the same sum |
26+
| 19 | [Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/019_Remove_Nth_Node_From_End_of_List.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/019_Remove_Nth_Node_From_End_of_List.java) | 1. Go through list and get length, then remove length-n, O(n) and O(n)<br>2. Two pointers, first pointer goes to n position, then move both pointers until reach tail, O(n) and O(n) |
2627
| 20 | [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/020_Valid_Parentheses.py) | 1. Stack<br>2. Replace all parentheses with '', if empty then True |
2728
| 21 | [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/021_Merge_Two_Sorted_Lists.py) | Add a dummy head, then merge two sorted list in O(m+n) |
2829
| 23 | [Merge k Sorted Lists](https://leetcode.com/problems/merge-k-sorted-lists/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/023_Merge_k_Sorted_Lists.py) | 1. Priority queue O(nk log k)<br>2. Binary merge O(nk log k)| |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*Given a linked list, remove the n-th node from the end of list and return its head.
2+
3+
Example:
4+
Given linked list: 1->2->3->4->5, and n = 2.
5+
After removing the second node from the end, the linked list becomes 1->2->3->5.
6+
7+
Note:
8+
Given n will always be valid.
9+
10+
Follow up:
11+
Could you do this in one pass?*/
12+
13+
/**
14+
* Definition for singly-linked list.
15+
* public class ListNode {
16+
* int val;
17+
* ListNode next;
18+
* ListNode(int x) { val = x; }
19+
* }
20+
*/
21+
class Solution {
22+
/*public ListNode removeNthFromEnd(ListNode head, int n) {
23+
ListNode curr = head;
24+
int ls = 0;
25+
while (curr != null) {
26+
curr = curr.next;
27+
ls++;
28+
}
29+
// n == len
30+
if (ls == n) {
31+
if (ls > 1) return head.next;
32+
return null;
33+
}
34+
curr = head;
35+
// Move to ls - n - 1
36+
for (int i = 0; i < ls - n - 1; i++) {
37+
curr = curr.next;
38+
}
39+
// Remove ls - n - 1
40+
curr.next = curr.next.next;
41+
return head;
42+
}*/
43+
44+
public ListNode removeNthFromEnd(ListNode head, int n) {
45+
ListNode slow, fast, curr;
46+
slow = head; fast = head;
47+
for (int i = 0; i < n; i++)
48+
fast = fast.next;
49+
// n == len
50+
if (fast == null) {
51+
head = head.next;
52+
return head;
53+
}
54+
// Move both pointers, until reach tail
55+
while (fast.next != null) {
56+
fast = fast.next;
57+
slow = slow.next;
58+
}
59+
curr = slow.next;
60+
slow.next = curr.next;
61+
return head;
62+
}
63+
}

python/019_Remove_Nth_Node_From_End_of_List.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
# Given a linked list, remove the n-th node from the end of list and return its head.
2+
#
3+
# Example:
4+
# Given linked list: 1->2->3->4->5, and n = 2.
5+
# After removing the second node from the end, the linked list becomes 1->2->3->5.
6+
#
7+
# Note:
8+
# Given n will always be valid.
9+
#
10+
# Follow up:
11+
# Could you do this in one pass?
12+
113
# Definition for singly-linked list.
214
# class ListNode(object):
315
# def __init__(self, x):
@@ -30,7 +42,6 @@ class Solution(object):
3042
# index[index_pos].next = index[index_pos + 1].next
3143
# return head
3244

33-
3445
def removeNthFromEnd(self, head, n):
3546
# https://leetcode.com/discuss/86721/o-n-solution-in-java
3647
if head is None:

0 commit comments

Comments
 (0)