MCPcopy Create free account
hub / github.com/Blankj/awesome-java-leetcode / Solution

Class Solution

src/com/blankj/medium/_0019/Solution.java:13–37  ·  view source on GitHub ↗

author: Blankj blog : http://blankj.com time : 2017/04/27 desc :

Source from the content-addressed store, hash-verified

11 * </pre>
12 */
13public class Solution {
14 public ListNode removeNthFromEnd(ListNode head, int n) {
15 ListNode pre = head;
16 ListNode afterPreN = head;
17 while (n-- != 0) {
18 afterPreN = afterPreN.next;
19 }
20 if (afterPreN != null) {
21 while (afterPreN.next != null) {
22 pre = pre.next;
23 afterPreN = afterPreN.next;
24 }
25 pre.next = pre.next.next;
26 } else {
27 head = head.next;
28 }
29 return head;
30 }
31
32 public static void main(String[] args) {
33 Solution solution = new Solution();
34 ListNode.print(solution.removeNthFromEnd(ListNode.createTestData("[1,2,3,4,5]"), 2));
35 ListNode.print(solution.removeNthFromEnd(ListNode.createTestData("[1]"), 1));
36 }
37}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected