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;
Image of page 1
Image of page 2

You've reached the end of your free preview.

Want to read both pages?

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

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes