-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveNthNode.java
More file actions
43 lines (41 loc) · 1.02 KB
/
RemoveNthNode.java
File metadata and controls
43 lines (41 loc) · 1.02 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
/********************************************************
> File Name:19RemoveNthNode.java
> Auther: ihochang
> Mail: yihez@andrew.cmu.edu
> Created Time: Thu Jan 7 20:14:57 2016
*********************************************************/
public class RemoveNthNode {
public ListNode removeNthFromEnd(ListNode head,int n) {
ListNode findAll = head;
ListNode findNth = head;
int i=0;
while(findAll!=null) {
findAll = findAll.next;
i++;
if (i > n+1) {// find the node before Nth node
findNth = findNth.next;
}
}
if (i==n) {
head = findNth.next;
} else {
findNth.next = findNth.next.next;
}
return head;
}
public static void main(String[] args) {
RemoveNthNode so = new RemoveNthNode();
ListNode head = new ListNode(1);
ListNode begin = head;
for (int i = 2;i<6;i++) {
head.next = new ListNode(i);
head = head.next;
}
head = begin;
ListNode newNode = so.removeNthFromEnd(head,1);
while(newNode!=null) {
System.out.println(newNode.val);
newNode = newNode.next;
}
}
}