- Удалить средний узел связанного списка. Решено с использованием Java. Единственная разница между этими решениями — использование операторов printf. время выполнения решения printf ~ 1100 мс против 4 мс без
Распечатать утверждения влияют на производительность в такой степени?
1)
public ListNode deleteMiddle(ListNode head) {
ListNode fast, slow;
if(head.next != null){
fast = slow = head;
try{
while(fast.next.next != null){
System.out.printf("Fastpre: %d ", fast.val);
fast = fast.next.next;
System.out.printf("Fastpost: %d\n", fast.val);
System.out.printf("Slowpre: %d ", slow.val);
if(fast.next != null)
slow = slow.next;
System.out.printf("Slowpost: %d\n", slow.val);
}
}
catch (Exception e){
}
slow.next = slow.next.next;
}
else
head = null;
return head;
}
VS
2)
public ListNode deleteMiddle(ListNode head) {
ListNode fast, slow;
if(head.next != null){
fast = slow = head;
try{
while(fast.next.next != null){
fast = fast.next.next;
if(fast.next != null)
slow = slow.next;
}
}
catch (Exception e){
System.out.println("Fast reached the end");
}
slow.next = slow.next.next;
}
else
head = null;
return head;
}
Подробнее здесь: https://stackoverflow.com/questions/788 ... e-accepted