Ejemplo n.º 1
0
  public static void reorderList(ListNode head) {
    if (head == null || head.next == null || head.next.next == null) return;

    ListNode n = head;
    int len = 1;
    while (n.next != null) {
      n = n.next;
      len++;
    }

    ListNode partition = head;
    for (int i = 0; i < len / 2 - 1; i++) partition = partition.next;
    ListNode second = partition.next;
    partition.next = null;
    second = reverse(second);

    ListNode first = head;
    ListNode dummy = new ListNode(-1);
    while (first != null && second != null) {
      dummy.next = first;
      first = first.next;
      dummy.next.next = second;
      second = second.next;
      dummy = dummy.next.next;
    }
    dummy.next = first == null ? second : first;
  }
Ejemplo n.º 2
0
  public static void main(String[] args) {
    ListNode l = new ListNode(1);
    l.next = new ListNode(2);
    l.next.next = new ListNode(3);
    l.next.next.next = new ListNode(4);
    l.next.next.next.next = new ListNode(5);
    l.display();

    reorderList(l);
    l.display();
  }
Ejemplo n.º 3
0
 public static ListNode reverse(ListNode head) {
   if (head == null || head.next == null) return head;
   ListNode prev = null;
   ListNode curr = head;
   while (curr != null) {
     ListNode next = curr.next;
     curr.next = prev;
     prev = curr;
     curr = next;
   }
   return prev;
 }