Advertisement
Guest User

Grokking 238

a guest
Oct 20th, 2022
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.49 KB | None | 0 0
  1. class Solution {
  2.     public ListNode removeNthFromEnd(ListNode head, int n) {
  3.    
  4.         ListNode start = new ListNode(-1);
  5.         ListNode slow = start;
  6.         ListNode fast = start;
  7.         slow.next = head;
  8.  
  9.         for (int i = 0; i < n + 1; i++) {
  10.             fast = fast.next;
  11.         }
  12.        
  13.         while (fast != null) {
  14.             slow = slow.next;
  15.             fast = fast.next;
  16.         }
  17.        
  18.         slow.next = slow.next.next;
  19.         return start.next;
  20.     }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement