-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathremoveNthFromEnd.java
More file actions
executable file
·54 lines (49 loc) · 1.13 KB
/
removeNthFromEnd.java
File metadata and controls
executable file
·54 lines (49 loc) · 1.13 KB
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
47
48
49
50
51
52
53
54
/*
Given a linked list, remove the nth node from the end of list and return its head.
For example,
Given linked list: 1->2->3->4->5, and n = 2.
After removing the second node from the end, the linked list becomes 1->2->3->5.
Note:
Given n will always be valid.
Try to do this in one pass.
*/
import java.util.*;
class ListNode{
int val;
ListNode next;
ListNode(int x){
val = x;
next = null;
}
}
public class removeNthFromEnd{
public static void main(String[] args) {
ListNode l1 = new ListNode(5);
l1.next = new ListNode(9);
l1.next.next = new ListNode(8);
ListNode res = removeNthFromEnd(l1,2);
while (res != null){
System.out.print(res.val + "->");
res = res.next;
}
}
public static ListNode removeNthFromEnd(ListNode head, int n){
if(head == null) return null;
if(head.next == null && n == 1)
return null;
ListNode ph = head;
ListNode p = head;
int count = 0;
while(ph.next != null){
if(count >= n)
p = p.next;
count ++;
ph = ph.next;
}
if(count < n)
head = head.next;
else
p.next = p.next.next;
return head;
}
}