GeezerVison is showing github.com — 019_Remove_Nth_Node_From_End_of_List · zhimintu/leetcode@3d15735
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: README.md +1Lines changed: 1 addition & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -23,6 +23,7 @@ Also, there are open source implementations for basic data structs and algorithm | 23 | 23 | | 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) | | 24 | 24 | | 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| | 25 | 25 | | 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) | | 26 | 27 | | 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 | | 27 | 28 | | 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) | | 28 | 29 | | 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)| | | |
Collapse file Copy file name to clipboard +63Lines changed: 63 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 | +} |
Collapse file Copy file name to clipboardExpand all lines: python/019_Remove_Nth_Node_From_End_of_List.py +12-1Lines changed: 12 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| | 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 | + | 1 | 13 | # Definition for singly-linked list. | 2 | 14 | # class ListNode(object): | 3 | 15 | # def __init__(self, x): | @@ -30,7 +42,6 @@ class Solution(object): | 30 | 42 | # index[index_pos].next = index[index_pos + 1].next | 31 | 43 | # return head | 32 | 44 |
| 33 | | - | 34 | 45 | def removeNthFromEnd(self, head, n): | 35 | 46 | # https://leetcode.com/discuss/86721/o-n-solution-in-java | 36 | 47 | if head is None: | |
|