Back to Front Linked List - Amazon Top Interview Questions


Problem Statement :


Given a singly linked list node, reorder it such that we take: the last node, and then the first node, and then the second last node, and then the second node, etc.

Can you do it in \mathcal{O}(1)O(1) space?

Constraints

0 ≤ n ≤ 100,000 where n is the number of nodes in node

Example 1

Input

node = [0, 1, 2, 3]

Output

[3, 0, 2, 1]



Solution :



title-img




                        Solution in C++ :

LLNode* solve(LLNode* node) {
    LLNode* slow = node;
    LLNode* fast = node;
    while (fast) {
        fast = fast->next;
        if (fast) {
            fast = fast->next;
            slow = slow->next;
        }
    }
    LLNode* pre = NULL;
    LLNode* cur = slow;
    LLNode* nex;
    while (cur) {
        nex = cur->next;
        cur->next = pre;
        pre = cur;
        cur = nex;
    }
    LLNode* temp = pre;
    while (temp && node) {
        LLNode* nex1 = temp->next;
        LLNode* nex2 = node->next;
        temp->next = node;
        node->next = nex1;
        temp = nex1;
        node = nex2;
    }
    return pre;
}
                    


                        Solution in Java :

import java.util.*;

/**
 * class LLNode {
 *   int val;
 *   LLNode next;
 * }
 */
class Solution {
    public LLNode solve(LLNode node) {
        LLNode fast = node, slow = node;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        LLNode left = node, right = reverse(slow);
        LLNode prev = new LLNode();
        LLNode dummy = prev;
        while (left != null && right != null) {
            prev.next = right;
            right = right.next;
            prev.next.next = left;
            left = left.next;
            prev = prev.next.next;
        }
        prev.next = null;
        return dummy.next;
    }

    private LLNode reverse(LLNode node) {
        LLNode prev = null, curr = node, next;
        while (curr != null) {
            next = curr.next;
            curr.next = prev;
            prev = curr;
            curr = next;
        }
        return prev;
    }
}
                    


                        Solution in Python : 
                            
class Solution:
    def split(self, head):
        tail1 = head
        tail2 = head

        while tail1 and tail2 and tail2.next and tail2.next.next:
            tail1 = tail1.next
            tail2 = tail2.next.next

        # now actually break the link!
        head2 = tail1.next
        tail1.next = None

        return head, head2

    def reverse(self, head):
        if not head or not head.next:
            return head
        prev, cur = None, head
        while cur:
            nxt = cur.next
            cur.next = prev
            prev = cur
            cur = nxt

        return prev

    def interleave(self, head, head2):
        resHead = res = LLNode(0)
        while head or head2:
            if head2:
                res.next = head2
                res = res.next
                head2 = head2.next

            if head:
                res.next = head
                res = res.next
                head = head.next

        return resHead.next

    def solve(self, head):
        # break into two halves
        head, head2 = self.split(head)

        # now reverse the second list ;)
        head2 = self.reverse(head2)

        # just alternately pick from each
        return self.interleave(head, head2)
                    


View More Similar Problems

Contacts

We're going to make our own Contacts application! The application must perform two types of operations: 1 . add name, where name is a string denoting a contact name. This must store name as a new contact in the application. find partial, where partial is a string denoting a partial name to search the application for. It must count the number of contacts starting partial with and print the co

View Solution →

No Prefix Set

There is a given list of strings where each string contains only lowercase letters from a - j, inclusive. The set of strings is said to be a GOOD SET if no string is a prefix of another string. In this case, print GOOD SET. Otherwise, print BAD SET on the first line followed by the string being checked. Note If two strings are identical, they are prefixes of each other. Function Descriptio

View Solution →

Cube Summation

You are given a 3-D Matrix in which each block contains 0 initially. The first block is defined by the coordinate (1,1,1) and the last block is defined by the coordinate (N,N,N). There are two types of queries. UPDATE x y z W updates the value of block (x,y,z) to W. QUERY x1 y1 z1 x2 y2 z2 calculates the sum of the value of blocks whose x coordinate is between x1 and x2 (inclusive), y coor

View Solution →

Direct Connections

Enter-View ( EV ) is a linear, street-like country. By linear, we mean all the cities of the country are placed on a single straight line - the x -axis. Thus every city's position can be defined by a single coordinate, xi, the distance from the left borderline of the country. You can treat all cities as single points. Unfortunately, the dictator of telecommunication of EV (Mr. S. Treat Jr.) do

View Solution →

Subsequence Weighting

A subsequence of a sequence is a sequence which is obtained by deleting zero or more elements from the sequence. You are given a sequence A in which every element is a pair of integers i.e A = [(a1, w1), (a2, w2),..., (aN, wN)]. For a subseqence B = [(b1, v1), (b2, v2), ...., (bM, vM)] of the given sequence : We call it increasing if for every i (1 <= i < M ) , bi < bi+1. Weight(B) =

View Solution →

Kindergarten Adventures

Meera teaches a class of n students, and every day in her classroom is an adventure. Today is drawing day! The students are sitting around a round table, and they are numbered from 1 to n in the clockwise direction. This means that the students are numbered 1, 2, 3, . . . , n-1, n, and students 1 and n are sitting next to each other. After letting the students draw for a certain period of ti

View Solution →