Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Given a singly linked list, find the middle node of the linked list and move that node to the head of the list. Return the head of the list. In the case of a list with an even number of nodes, use the second middle one.
- Input format
- There are 2 lines of input
- N - An integer denoting the number of nodes in the linked list.
- N integers follow where ith integer denotes the ith node value in the linked list
- Output format
- Return the head of the modified list after moving the middle node to head.
- Constraints
- 0 <= N<= 10^5
- -10^9 <= value <= 10^9
- Sample Input 1
- 6
- 2 3 4 5 6 7
- Sample Output 1
- 5 2 3 4 6 7
- Explanation 1
- The middle node here is [5]. Moving that to the head of the list gives us the output shown.
- Sample Input 2
- 5
- 1 2 3 4 5
- Sample Output 2
- 3 1 2 4 5
- Explanation 2
- The middle node here is [3]. Moving that to the head of the list gives us the output shown.
- */
- /*
- class ListNode{
- constructor(val){
- this.val = val;
- this.next = null;
- }
- */
- /**
- * @param {ListNode} head
- * @return {ListNode}
- */
- function moveMiddleToHead(head) {
- let slow, slow_prev,fast;
- slow=head;
- slow_prev=null;
- fast=head;
- if(head==null||head.next==null)
- return head;
- // let count=0;
- while(slow!=null&&fast!=null){
- if(fast.next==null)
- break;
- // count++;
- slow_prev=slow;
- slow=slow.next;
- fast=fast.next.next;
- }
- // console.log("iteration count=",count);
- // console.log("slow.val",slow.val)
- // console.log("slow.next.val=",slow.next.val);
- // console.log("slow_prev",slow_prev.val)
- // console.log("fast",fast.val)
- let slow_next=slow.next;
- slow.next=head;
- slow_prev.next=slow_next;
- head=slow;
- return head;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement