“`”

LinkedList没有提供反转链表的相关函数,以下是通过foreach实现链表反转

<pre><code class=""language-java"" lang=""java"">static LinkedList reverseLinkedList(LinkedList linkedList) {
LinkedList<Object> newLinkedList = new LinkedList<>();
for (Object object : linkedList) {
newLinkedList.add(0, object);
}
return newLinkedList;
}
</code></pre>

 

<pre><code> "“`

Was this helpful?

0 / 0

发表回复 0

Your email address will not be published.