-
🎵 19. Remove Nth Node From End of List javaLeetCode_Study_Plan/Algorithm 2022. 12. 21. 00:42
https://leetcode.com/problems/remove-nth-node-from-end-of-list/
새로운 ListNode를 생성해도 head 이후의 next까지만 연결해놓고 return을 하기에 의도한 값이 아님.
추가 해결 필요
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solution { public ListNode removeNthFromEnd(ListNode head, int n) { ListNode temp = head; int total = 0; for (; temp != null; temp = temp.next) { total++; } for (int i = 1; head != null; head = head.next) { if (i == total - n) { ListNode next = head.next.next; head.next = next; break; } i++; } return head; } }
'LeetCode_Study_Plan > Algorithm' 카테고리의 다른 글
3. Longest Substring Without Repeating Characters java (0) 2022.12.21 567. Permutation in String java (0) 2022.12.21 876. Middle of the Linked List java (0) 2022.12.21 🎵 557. Reverse Words in a String III java (0) 2022.12.20 344. Reverse String java (0) 2022.12.20