-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveNode.java
More file actions
112 lines (101 loc) · 2.53 KB
/
RemoveNode.java
File metadata and controls
112 lines (101 loc) · 2.53 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import java.util.List;
/**
* Created by Administrator on 2016/5/25.
*/
public class RemoveNode {
static class ListNode{
int val;
ListNode next;
ListNode(int x){
val = x;
}
}
public static void main(String[] args){
ListNode node1 = new ListNode(1);
ListNode node2 = new ListNode(2);
// ListNode node3 = new ListNode(3);
node1.next = node2;
node2.next =null;
RemoveNode ll =new RemoveNode();
ListNode l1 = ll.removeNthFromEnd(node1,2);
while(l1!=null){
System.out.print(l1.val);
l1 = l1.next;
}
}
public static ListNode removeNthFromEnd(ListNode head, int n) {
int length = 1;
int i=0,j=0;
ListNode first = head;
while(head.next!=null) {
head = head.next;
length++;
}
head = first;
while(i<(length-n)-1){
i++;
head = head.next;
}
if(length-n-1<0)
{
first =first.next;
}
if(head.next!=null) {
head.next = head.next.next;
return first;
}else {
return null;
}
}
//public ListNode removeNthFromEnd(ListNode head, int n) {
// ListNode dummy = new ListNode(0);
// dummy.next = head;
// int length = 0;
// ListNode first = head;
// while (first != null) {
// length++;
// first = first.next;
// }
// length -= n;
// first = dummy;
// while (length > 0) {
// length--;
// first = first.next;
// }
// first.next = first.next.next;
// return dummy.next;
// }
//public ListNode removeNthFromEnd(ListNode head, int n){
//
// int i=0;
// int length = 1;
// int j=0;
// ListNode tmp = head;
// while (head.next!=null)
// {
// head =head.next;
// length++;
// }
//// System.out.print(length);
// head = tmp;
// if(n==1){
// while(i<length-1){
//
// head =head.next;
// i++;
// }
// if(head==tmp)
// tmp=null;
// System.out.print(head.val);
// System.out.print(head.next);
// head = null;
// } else{
// while(j<length-n-1){
// tmp =tmp.next;
// j++;
// }
// tmp.next =tmp.next.next;
// }
// return tmp;
// }
}