MCPcopy Create free account
hub / github.com/apna-college/Alpha / removeNthfromEnd

Method removeNthfromEnd

12_LinkedList.java/LL.java:152–174  ·  view source on GitHub ↗
(int n)

Source from the content-addressed store, hash-verified

150 }
151
152 public void removeNthfromEnd(int n) {
153 //size
154 int size = 0;
155 Node temp = head;
156 while(temp != null) {
157 temp = temp.next;
158 size++;
159 }
160 //if we have to remove the head
161 if(n == size) {
162 head = head.next;
163 return;
164 }
165
166 int i = 1;
167 int itf = size-n;
168 Node prev = head;
169 while(i < itf) { //try to find the node previous of nth
170 prev = prev.next;
171 i++;
172 }
173 prev.next = prev.next.next;
174 }
175
176 private Node findMidNode(Node head) {
177 Node slow = head;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected