Untitled
unknown
java
a year ago
1.0 kB
2
Indexable
Never
public class LongestNonIncreasingSublist { public int[] findLongestList(ListNode head) { ListNode curSublistHead = head, longestHead = head; int longest = head == null ? 0 : 1, curSublistLen = longest; while (head.next != null) { if (head.val >= head.next.val) { ++curSublistLen; }else { if (longest < curSublistLen) { longest = curSublistLen; longestHead = curSublistHead; } curSublistLen = 1; curSublistHead = head.next; } head = head.next; } if (longest < curSublistLen) { longestHead = curSublistHead; longest = curSublistLen; } int[] ans = new int[longest]; for (int i = 0; i < longest; ++i) { ans[i] = longestHead.val; longestHead = longestHead.next; } return ans; }