java control flow 求救

F_郭爾軍

36 回覆
0 Like 6 Dislike
F_郭爾軍 2023-02-26 01:10:14
given a linked list, e.g.
we need to iterate through all nodes and print all value, and the pointer should stop at the last valid node
how to do it without using break statement in while loop?


// this will miss the last node
while (head.next != null) {
    System.out.println(head.val);
    head = head.next;
}

// the pointer will stop on null
while (head != null) {
    System.out.println(head.val);
    head = head.next;
}


// "work" but doesnt look nice...
while (head.next != null) {
    System.out.println(head.val);
    head = head.next;
}
System.out.println(head.val);

// "work" but doesnt look nice...
Node prev;
while (head = null) {
    System.out.println(head.val);
    prev = head;
    head = head.next;
}
System.out.println(head.val);
return prev;


// "work" but doesnt look nice...
while (true) {
    System.out.println(head.val);
    if (head.next != null) head = head.next;
    else break;
}
徳田重男 2023-02-26 01:13:47
To iterate through all nodes in a linked list and print all values, and stop at the last valid node without using a break statement in a while loop in Java, you can use a boolean flag to keep track of whether you have reached the last valid node. Here's an example code snippet that demonstrates this approach:


// Assuming that you have a singly linked list with a head node
Node currentNode = head;
boolean isLastNodeReached = false;

while (currentNode != null && !isLastNodeReached) {
    System.out.println(currentNode.value);
    if (currentNode.next == null) {
        isLastNodeReached = true;
    }
    currentNode = currentNode.next;
} 

In this code, we start by setting the current node to the head node of the linked list. We also initialize a boolean flag isLastNodeReached to false.

We then enter a while loop that will continue as long as there are more nodes in the linked list and we haven't reached the last valid node.

Inside the while loop, we print the value of the current node and then check whether the next node is null. If it is, then we set the isLastNodeReached flag to true, indicating that we have reached the last valid node.

Finally, we update the currentNode variable to point to the next node in the linked list.

By using this boolean flag approach, we can avoid using a break statement in the while loop and still stop at the last valid node in the linked list.
徳田重男 2023-02-26 01:15:11
Yes, it is possible to iterate through all nodes in a linked list and print all values, and stop at the last valid node without using a boolean flag. One approach is to use a do-while loop instead of a while loop. Here's an example code snippet that demonstrates this approach:

// Assuming that you have a singly linked list with a head node
Node currentNode = head;

do {
    System.out.println(currentNode.value);
    currentNode = currentNode.next;
} while (currentNode != null && currentNode.next != null);
F_郭爾軍 2023-02-26 01:22:40
this will miss the last node, where currentNode.next is null
F_郭爾軍 2023-02-26 01:24:18
the currentNode ptr will stop on null
also doesnt really like the idea of using a boolean flag...
含我呀 2023-02-26 01:26:12
looks nice既定義係? 效能上同可讀性上同上面啲work既例子有冇太大分別?
伙儕仁 2023-02-26 01:26:51
println(head.next.val) ?
伙儕仁 2023-02-26 01:28:00
自膠Miss左頭果個
F_郭爾軍 2023-02-26 01:31:52
time complexity all are O(n);
避免用break statement 係為左可讀性;
其餘用兩個ptr or print value after loop唔係唔得既, 係想知有冇更好辦法
反中共係常識 2023-02-26 01:40:30
Two pointer 問題嚟?

一個pointer做print?
另一個行快一格check下洗唔洗terminate個loop?
F_郭爾軍 2023-02-26 01:40:44
tried several time, keep returning either two ptr approach, break statement approach or simply stopping on null node
F_郭爾軍 2023-02-26 01:43:13
唔係唔得,係想知有冇更直接做法
可能唯有係咁
反中共係常識 2023-02-26 01:56:30
樓主不停強調可讀性 但你心目中嘅可讀性係點

小弟就不嬲覺得係嚟自改得好嘅function名同variable名
同埋唔好一大段撈埋一齊,要用function斬件

喺呢條問題嘅話用recursive function都可以係一個出路,點睇
含我呀 2023-02-26 02:01:37
當你段code咁短咁簡單,加個break基本上好難影響可讀性
含我呀 2023-02-26 02:08:48
用recursive已經係開始變成over-engineering
F_郭爾軍 2023-02-26 02:11:23
of course it doesnt make much difference in the current example given the small size of the programe;
it might be a personal preference, in general I avoid using break outside switch statement
https://users.csc.calpoly.edu/~jdalbey/SWE/Papers/att_collapse.html
反中共係常識 2023-02-26 02:14:16
recursive點會over engineering

讀書個陣逢有linked list都係用recursive多
F_郭爾軍 2023-02-26 02:19:33
iterative vs recursive
if i recall correctly, iterative take up memory from heap
含我呀 2023-02-26 02:21:42
所以我既睇法係視乎情況用,唔係話一用就係唔好。你link既例子明顯唔適用係因為while loop入面再有switch,break會令人混淆,呢啲情況就要小心,又或者反而係避免while入面用switch,改用if else,或者用function call代替
含我呀 2023-02-26 02:28:17
如果唔係algorithm需要,同一情況下原地loop一定快過recursive,recursive每行一次要initialize function call,要copy input variable,要用時間 & memory,同埋你一唔小心係可以不知不覺用左好多memory,特別係你data量大既時間
_____K 2023-02-26 02:52:31
有無試過用sentinel node?
Node sentinel = new Node(); sentinel.next = head; while (sentinel.next != null) { print(sentinel.next.val); sentinel = sentinel.next; }
行完 sentinel 應該會指住最後個 non null node
F_郭爾軍 2023-02-26 03:09:36
Node sentinel = new Node(); 
sentinel.next = head; 
while (sentinel.next != null) {
    print(sentinel.next.val); 
    sentinel = sentinel.next; 
} 

interesting alternative, cleaner than the original two ptr approach (which has typo, now fixed):
Node prev = head;
while (head != null) {
    System.out.println(head.val);
    prev = head;
    head = head.next;
}
_____K 2023-02-26 03:12:39
咁 two pointer 應該好D,如果 head 一開波可以係 null 的話,two pointer 果個方法仍然正確。
F_郭爾軍 2023-02-26 03:18:36
seems your code is working fine:
if head == null, sentinel.next would be pointing at null, and not printing anything; when finish, sentinel.next is indeed pointing at null, which is exactly what we want
吹水台自選台熱 門最 新手機台時事台政事台World體育台娛樂台動漫台Apps台遊戲台影視台講故台健康台感情台家庭台潮流台美容台上班台財經台房屋台飲食台旅遊台學術台校園台汽車台音樂台創意台硬件台電器台攝影台玩具台寵物台軟件台活動台電訊台直播台站務台黑 洞