Remove Nth Node From End of List Solutions in Java
Number 19
Difficulty Medium
Acceptance 35.2%
Link LeetCode
Solutions
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/remove-nth-node-from-end-of-list/description//// Author : liuyubobobo/// Time : 2017-11-15//// Time Complexity: O(n)// Space Complexity: O(1)public class Solution1 {public ListNode removeNthFromEnd(ListNode head, int n) {ListNode dummyHead = new ListNode(0);dummyHead.next = head;int length = 0;for(ListNode cur = dummyHead.next ; cur != null ; cur = cur.next)length ++;int k = length - n;assert k >= 0;ListNode cur = dummyHead;for(int i = 0 ; i < k ; i ++)cur = cur.next;cur.next = cur.next.next;return dummyHead.next;}public static void main(String[] args) {int arr[] = {1, 2, 3, 4, 5};ListNode head = new ListNode(arr);System.out.println(head);head = (new Solution1()).removeNthFromEnd(head, 2);System.out.println(head);}}
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/remove-nth-node-from-end-of-list/description//// Author : liuyubobobo/// Time : 2017-11-15//// Two Pointers - One Pass Algorithm// Time Complexity: O(n)// Space Complexity: O(1)public class Solution2 {public ListNode removeNthFromEnd(ListNode head, int n) {ListNode dummyHead = new ListNode(0);dummyHead.next = head;ListNode p = dummyHead;ListNode q = dummyHead;for( int i = 0 ; i < n + 1 ; i ++ ){assert q != null;q = q.next;}while(q != null){p = p.next;q = q.next;}p.next = p.next.next;return dummyHead.next;}public static void main(String[] args) {int arr[] = {1, 2, 3, 4, 5};ListNode head = new ListNode(arr);System.out.println(head);head = (new Solution2()).removeNthFromEnd(head, 2);System.out.println(head);}}