题解 | #重排链表#

重排链表

http://www.nowcoder.com/practice/3d281dc0b3704347846a110bf561ef6b

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public void reorderList(ListNode head) {
        if(head == null) return;
        if(head.next == null) return;
        if(head.next.next == null) return;
        ListNode second = head.next;
        ListNode tail = head.next.next;
        ListNode pre = head.next;
        while(tail.next != null){
            pre = tail;
            tail = tail.next;
        }
        pre.next = null;
        head.next = tail;
        tail.next = second;
        reorderList(second);
    }
}
全部评论

相关推荐

牛客ID:561366855:期望薪资多少?难以相信这简历找不到工作。说明二本电子信息专业想对口就业非常难。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务