Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const head1 = {
- val:1,
- next:{
- val:2,
- next:{
- val:3,
- next: {
- val:4,
- next:{
- val:5,
- next:null
- }
- }
- }
- }
- }
- const removeNafterM = (head, N, M) => {
- let counter = 1
- let ref = head
- while(counter < N) {
- if(ref.next ===null){
- throw new Error('Starting point outside the list')
- }
- ref = ref.next
- counter++
- }
- secondRef = ref
- finalCounter = counter+M
- while(counter<=finalCounter) {
- if(secondRef.next === null){
- ref.next = null
- return head
- }
- secondRef = secondRef.next
- counter++
- }
- ref.next = secondRef;
- return head;
- }
- console.log(removeNafterM(head1,10,2));
Advertisement
Add Comment
Please, Sign In to add comment