-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremoveNthNode.java
46 lines (40 loc) · 1.35 KB
/
removeNthNode.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* 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 iterator1 = head;
ListNode iterator2 = moveIterator(head, n);
if(isTail(iterator1)){System.out.print("CASE 1"); return null;}
else if(iterator2==null){head = iterator1.next;System.out.print("CASE 2"); return head;}
while(iterator2.next !=null){
iterator2 = iterator2.next;
iterator1 = iterator1.next;
}
if(nextElementIsTail(iterator1)){iterator1.next = null;System.out.print("CASE 3");}
else {iterator1.next = iterator1.next.next;System.out.print("CASE 4");}
return head;
}
public boolean nextElementIsTail(ListNode list){
return list.next.next==null;
}
public boolean isTail(ListNode list){
return list.next==null;
}
public ListNode moveIterator(ListNode head, int n){
ListNode iterator = head;
int index = 0;
while(index<n){
iterator = iterator.next;
index = index+1;
}
return iterator;
}
}