Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcf3fd79

Browse files
one pass approach for " remove nth node from end of list "
1 parent0f351ba commitcf3fd79

File tree

1 file changed

+55
-2
lines changed

1 file changed

+55
-2
lines changed

‎EASY/src/easy/RemoveNthNodeFromEndOfList.java

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@
1818
Given n will always be valid.
1919
Try to do this in one pass.*/
2020
publicclassRemoveNthNodeFromEndOfList {
21-
publicListNoderemoveNthFromEnd(ListNodehead,intn) {
21+
22+
/**Naive/most straightforward approach:
23+
* go through the list, find its total length, then go through the list a second time:
24+
* this time, pause at the delta point, then assign its next.next pointer to next.
25+
* This approach has to traverse the list twice, not one-pass.*/
26+
publicListNoderemoveNthFromEnd_two_passes(ListNodehead,intn) {
2227
ListNodetemp =head;
2328
intlen =0;
2429
while(temp !=null){
@@ -40,10 +45,58 @@ public ListNode removeNthFromEnd(ListNode head, int n) {
4045
}
4146

4247
publicstaticvoidmain(String...strings){
48+
intn =2;
4349
ListNodehead =newListNode(1);
4450
head.next =newListNode(2);
4551
RemoveNthNodeFromEndOfListtest =newRemoveNthNodeFromEndOfList();
46-
ListNoderes =test.removeNthFromEnd(head,2);
52+
// ListNode res = test.removeNthFromEnd_two_passes(head, n);
53+
ListNoderes =test.removeNthFromEnd_one_pass(head,n);
4754
CommonUtils.printList(res);
4855
}
56+
57+
publicListNoderemoveNthFromEnd_one_pass(ListNodehead,intn) {
58+
//this approach uses two pointers, fast moves first for n nodes, when fast reaches n, then we start to move slow
59+
//then, when fast reaches null, slow reaches the point where the node should be deleted.
60+
ListNodedummy =newListNode(-1);
61+
dummy.next =head;
62+
ListNodeslow =head,fast =head;
63+
inttempN =n;
64+
while(tempN-- >0){
65+
fast =fast.next;
66+
}
67+
68+
if(fast ==null) {
69+
if(n >0) {
70+
// this is for cases like this: [1,2] 2 or [1,2,3,4] 4, namely, remove the head of
71+
// the list and return the second node from the original list
72+
dummy.next =dummy.next.next;
73+
}
74+
returndummy.next;
75+
}
76+
77+
fast =fast.next;//we'll have to move fast pointer one node forward before moving the two together, this way,
78+
//when fast reaches null, slow will be at the previous node to the node that should be deleted, thus, we can change the next pointer easily
79+
80+
while(fast !=null){
81+
fast =fast.next;
82+
slow =slow.next;
83+
}
84+
85+
if(slow.next !=null)slow.next =slow.next.next;
86+
returndummy.next;
87+
}
88+
89+
//a more concise version using the same idea found on Discuss
90+
publicListNoderemoveNthFromEnd_one_pass_more_concise_version(ListNodehead,intn) {
91+
ListNodedummy =newListNode(-1);
92+
dummy.next =head;
93+
ListNodeslow =dummy,fast =dummy;
94+
while(fast.next !=null){
95+
if(n <=0)slow =slow.next;
96+
fast =fast.next;
97+
n--;
98+
}
99+
if(slow.next !=null)slow.next =slow.next.next;
100+
returndummy.next;
101+
}
49102
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp