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

Commit0f351ba

Browse files
some extra notes on remove linked list elements
1 parentc44cb4d commit0f351ba

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

‎EASY/src/easy/RemoveLinkedListElements.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ public class RemoveLinkedListElements {
1919
* 1. Eventually, we should return dummy.next as the final result.
2020
* 2. we assign head to curr, dummy to prev
2121
* 3. and then we use prev and curr to traverse through the list and do the work
22-
* 4. each time, we only move one node forward, so we don't need another while loop inside the while loop*/
22+
* 4. each time, we only move one node forward, so we don't need another while loop inside the while loop
23+
* 5. KEY: if(curr.val == val), then curr is the node we want to remove, so, we'll assign curr.next to prev.next, thus, prev won't have that node
24+
* else, we'll keep moving prev forward, so, just do prev = prev.next
25+
* but, for both cases, we'll also move curr forward, so we put curr = curr.next in the outside.
26+
*
27+
* */
2328
publicListNoderemoveElements(ListNodehead,intval) {
2429
ListNodedummy =newListNode(-1);
2530
dummy.next =head;
@@ -37,8 +42,14 @@ public ListNode removeElements(ListNode head, int val) {
3742

3843
publicstaticvoidmain(String...strings){
3944
RemoveLinkedListElementstest =newRemoveLinkedListElements();
40-
intval =1;
45+
intval =6;
4146
ListNodehead =newListNode(1);
47+
head.next =newListNode(2);
48+
head.next.next =newListNode(6);
49+
head.next.next.next =newListNode(3);
50+
head.next.next.next.next =newListNode(4);
51+
head.next.next.next.next.next =newListNode(5);
52+
head.next.next.next.next.next.next =newListNode(6);
4253
ListNoderes =test.removeElements(head,val);
4354
CommonUtils.printList(res);
4455
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp