# Leetcode_019_Remove_Nth_Node_From_End_Of_List.java -...

• No School
• AA 1
• 2

This preview shows page 1 - 2 out of 2 pages.

package coding.leetcode; /** * * The whole point of this problem is that you can delete the head of the list and you really need sentinel. * * Created by Administrator on 2017-07-10. * 2019-02-15 에 다시 풀어봄 . * 기억할거 두가지 * 1. head delete 될수 있으니까 sentinel 준비하자 . * 2. slow 쪽에선 sentinel 부터 시작해야한다 . 이게 뒤에서 n+1 되는 포인트이기 때문에 여기서 next 를 다음다음으로 set 할수 있다 . */ public class Leetcode_019_Remove_Nth_Node_From_End_Of_List { public class ListNode { int val; ListNode next; ListNode(int x) { val = x; } } public ListNode removeNthFromEnd(ListNode head, int n) { ListNode sentinel = new ListNode(0); sentinel.next = head; ListNode fast = head; for(int i=0; i < n; i++){ fast = fast.next; } ListNode slow = sentinel; while(fast != null){ fast = fast.next; slow = slow.next; } if(slow != null) slow.next = slow.next.next; return sentinel.next; } private void test1(){ ListNode n1 = new ListNode(1); ListNode n2 = new ListNode(2); ListNode n3 = new ListNode(3); ListNode n4 = new ListNode(4); ListNode n5 = new ListNode(5); n1.next = n2;

#### You've reached the end of your free preview.

• Fall '19
• Slow, println, SENTINEL, public class Leetcode, head 도 delete, next 를 다음다음

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern