[B] [C] [D]

Given a head node head of singly linked list L **, singly linked list L can be expressed as:

L0 → L1 →... → Ln minus 1 → LnCopy the code

Please rearrange it to become:

L0 → Ln → L1 → ln-1 → L2 → ln-2 →...Copy the code

You can’t just change the values inside a node, but you need to actually swap nodes.

 

Example 1:

Input: head = [1,2,3,4]Copy the code

Example 2:

Input: head = [1,2,3,4,5] output: [1,5,2,4,3]Copy the code

Tip:

  • The length range of the linked list is[1, 5 * 104]
  • 1 <= node.val <= 1000

1->n->2-> N -1->…

To do this, you first need to take the nodes behind you, and do so from back to front

Based on this requirement, you can first turn the input list into a bidirectional list

We then define two Pointers pre and Next, which initially point to the head node and the tail node

Next = next, so that the first node points to the last node

Next = pre. Next, so that the last node points to the second node

Pre = pre. Next,next = next. Pre

Until the two Pointers meet, where we need to unlink the links in the list

Next = next or pre = next. At this point, the rings are actually on the node pointed by the next pointer. By setting next

The overall process is as follows:

The code is as follows:

var reorderList = function(head) { let pre = head,next = pre.next; While (next){next. Pre = pre; pre = next; next = pre? pre.next:null; } next = pre,pre = head; Next = next,next. Next = pre. Next. ==next && pre! ==next){ const pre_next = pre.next; pre.next = next; next.next = pre_next; pre = pre_next; next = next.pre; } // Next. Next = null; };Copy the code

At this point we are done with leetcode-143-rearranged linked lists

If you have any questions or suggestions, please leave a comment!