题解 | #重排链表#

重排链表

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

/**

  • Definition for singly-linked list.

  • class ListNode {

  • int val;
    
  • ListNode next;
    
  • ListNode(int x) {
    
  •     val = x;
    
  •     next = null;
    
  • }
    
  • } / import java.util.; public class Solution { public void reorderList(ListNode head) { if(head == null || head.next == null || head.next.next == null) return; List list = new ArrayList<>(); ListNode current = head; while (current != null) { list.add(current.val); current = current.next; } if(list.size()==1){

     }
     current = head;
     int i = 0;
     while (current != null) {
         current.val = list.get(i);
         current = current.next;
         if(current!=null){
             current.val = list.get(list.size() - 1 - i);
             current = current.next;
         }
         i++;
     }
    

    } }

全部评论

相关推荐

与火:这不接? 留子的钱不挣白不挣
点赞 评论 收藏
分享
死在JAVA的王小美:哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈,我也是,让我免了一轮,但是硬气拒绝了
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务