-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveNthNode.java
More file actions
58 lines (49 loc) · 1.21 KB
/
Copy pathRemoveNthNode.java
File metadata and controls
58 lines (49 loc) · 1.21 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
package com.wg.leetcode;
import com.wg.leetcode.MergeSortedList.ListNode;
/*
* 19,从链表末尾开始删除第n个节点,返回链表头部
*/
public class RemoveNthNode {
/**
* 思路:定义两个指针,一个先移动n-1 步,另一个指针从头结点开始,两个指针一起移动,当第一个指针移动到末尾时,第二个指针
* 就移动到了倒数第n个节点处,然后删除节点。
* @param head
* @param n
* @return
*/
public static ListNode removeNthFromEnd(ListNode head, int n){
if(head==null || n<0) return null;
if(head.next==null && n>=1)return null;
//定义两个指针
ListNode pNode1=head;
ListNode pNode2=head;
//第一个指针先移动n 步
//注意:n不能大于链表长度
for(int i=0;i<n;i++){
pNode1=pNode1.next;
}
//如果pNode1==null说明此时要删除的是头结点,所以head=head.next; 返回头结点
if(pNode1==null){
head=head.next;
return head;
}
//当pNode1!=null时,移动两个指针指针,
while(pNode1.next!=null){
pNode1=pNode1.next;
pNode2=pNode2.next;
}
//找到待删除的节点得前节点
if(pNode2.next.next==null)
pNode2.next=null;
else{
pNode2.next=pNode2.next.next;
}
return head;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}