Reverse Linked List

来源:互联网 发布:网络模式切换 编辑:程序博客网 时间:2024/06/07 00:59

Reverse a singly linked list.

题意:反转一个链表  比方 1→2→3→4 变成 4→3→2→1

自己画的一个图,一图胜千言嗯


注意代码的顺序,不能错

public ListNode reverseList(ListNode head) {if (null == head) return head;ListNode pre = head;ListNode post = pre.next;for (; head.next != null; ) {head.next = post.next;post.next = pre;//更新pre和postpre = post;post = head.next;}        return pre;}


0 0
原创粉丝点击