Solution 24: 链表翻转
生活随笔
收集整理的這篇文章主要介紹了
Solution 24: 链表翻转
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
程序
public class ListReverse {public ListNode reverseList(ListNode head) {if (head == null) {return head;}ListNode pre = head;ListNode cur = pre.next;pre.next = null;while (cur != null) {ListNode next = cur.next;cur.next = pre;pre = cur;cur = next;}return pre;}public ListNode reverseList2(ListNode head) {ListNode pre = null;ListNode cur = head;while (cur!=null) {ListNode next = cur.next;cur.next = pre;pre = cur;cur = next;}return pre;}
}
?
第一種是比較常見,第二種更為簡潔。
轉載于:https://www.cnblogs.com/harrygogo/p/4629483.html
總結
以上是生活随笔為你收集整理的Solution 24: 链表翻转的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 树知春不久归下一句是什么呢?
- 下一篇: 乌镇不要门票可以进去吗