<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Phanindra Allamsetty</title>
    <description>The latest articles on DEV Community by Phanindra Allamsetty (@phaniallamsetty).</description>
    <link>https://dev.to/phaniallamsetty</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F455341%2Fb2a4108c-f350-4abd-a224-ab02890b9486.jpeg</url>
      <title>DEV Community: Phanindra Allamsetty</title>
      <link>https://dev.to/phaniallamsetty</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/phaniallamsetty"/>
    <language>en</language>
    <item>
      <title>1. Arrays &amp; Hashing: Contains Duplicate</title>
      <dc:creator>Phanindra Allamsetty</dc:creator>
      <pubDate>Wed, 19 Mar 2025 14:45:13 +0000</pubDate>
      <link>https://dev.to/phaniallamsetty/1-contains-duplicate-51kb</link>
      <guid>https://dev.to/phaniallamsetty/1-contains-duplicate-51kb</guid>
      <description>&lt;p&gt;For a list of all coding patterns questions and tests, checkout my &lt;a href="https://github.com/phaniallamsetty/coding-patterns-practice" rel="noopener noreferrer"&gt;GitHub repo&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://leetcode.com/problems/contains-duplicate/description/" rel="noopener noreferrer"&gt;Problem:&lt;/a&gt; Contains Duplicate
&lt;/h2&gt;

&lt;p&gt;Given an integer array &lt;em&gt;nums&lt;/em&gt;, return &lt;em&gt;true _if any value appears at least twice in the array, and return _false&lt;/em&gt; if every element is distinct.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Example:
Input: nums = [1, 2, 3, 1]
Output: true
Explanation: The element 1 occurs at indices 0 and 3.

Example:
Input: nums = [1, 2, 3, 4]
Output: false
Explanation: No duplicate values are found.

Example:
Input: nums = [1, 1, 1, 3, 3, 4, 3, 2, 4, 2]
Output: true

Constraints:
- 1 &amp;lt;= nums.length &amp;lt;= 10^5
- -10^9 &amp;lt;= nums[i] &amp;lt;= 10^9
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are multiple ways of solving this problem. From the most naive solution, which is to run nested loops and compare if the same elements exists elsewhere to a less naive solution where you can sort the elements in the array and compare successive elements. If you see equal elements, you can return true. The nested loops method takes O(n^2) time whereas the sorting method takes O(nlong n) time. However, the space complexity id O(1) in both cases since we are using the same array to check for duplicates.&lt;/p&gt;

&lt;p&gt;Another approach which is of O(n) time complexity and O(n) space complexity is to use a hashset to track each element and to detect duplicates. Using Hashmaps and Hashsets is usually the best approach to detect duplicates.&lt;/p&gt;

&lt;p&gt;The way this approach works is that we create a hashset. For each element in the array, we check if it already exists in the hashset. If it does, we return true because we found a duplicate. If we don't find it, we add the element to the hashset and move on to the next one, where we do the same thing again.&lt;/p&gt;

&lt;p&gt;Time complexity: O(n)&lt;br&gt;
Space complexity: O(n)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public boolean containsDuplicate(int[] nums) {
    Set&amp;lt;Integer&amp;gt; numSet = new HashSet&amp;lt;&amp;gt;();

    for(int n: nums) {
        if(numSet.contains(n)) {
            return true;
        }

        numSet.add(n);
    }

    return false;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>neetcode150</category>
      <category>codingpatternsjava</category>
      <category>arrayshashing</category>
    </item>
    <item>
      <title>PRESTO card Metrolinx fare system</title>
      <dc:creator>Phanindra Allamsetty</dc:creator>
      <pubDate>Mon, 23 Dec 2024 13:24:21 +0000</pubDate>
      <link>https://dev.to/phaniallamsetty/presto-fare-system-3b2p</link>
      <guid>https://dev.to/phaniallamsetty/presto-fare-system-3b2p</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;PRESTO is the fare system for public transportation in the Greater Toronto Area. This system not only contains the city of Toronto but also other regional and city transit systems in the cities next to Toronto. Some of them are suburbs of Toronto whereas others are independent cities by themselves. In this post, we are trying to design the PRESTO ticket fare system.&lt;/p&gt;

&lt;p&gt;While the annual number of usage counts in all the networks is approximately 41 million, not all of them use the PRESTO system. Some of them use Credit Cards/Debit Cards, day ticket, weekend ticket, cash, etc. However, for the sake of simplicity we will assume that all of them use the PRESTO system.&lt;/p&gt;

&lt;h2&gt;
  
  
  Workflow
&lt;/h2&gt;

&lt;p&gt;The way this system works is a rider purchases a PRESTO card from one of the authorized vendors or the vending machines. Each PRESTO card has a number assigned to it to uniquely identify the card. The user can then login into the PRESTO app on their mobile or web and link that account with the card. A PRESTO account can have multiple cards linked to it. However, each PRESTO card can be linked to only one account. So there exists a one to many relationship between the user account and PRESTO card whereas a card to account relationship is one-to-one.&lt;/p&gt;

&lt;p&gt;After linking these cards to their accounts, the users can use either credit card or debit card or their bank account to their account. This data needs to be securely saved for auto-pay. The user can "recharge" their card with a certain amount and can use the different transportation services by tapping their PRESTO card to a terminal device at each bus stop, train station, etc. Once the user taps this card, a certain amount is deducted which is valid for 2 hours. The amount deducted differs from one transportation network to another. For example, for TTC, it is $3.30 whereas for UP it is $9. This fare is valid until 2 hours. One exception to this rule are GO Transit and UP which do not depend on the duration. So the user needs to tap the card at both the source and destination to mark a trip and be charged accordingly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Functional Requirements
&lt;/h2&gt;

&lt;p&gt;The functional requirements for the PRESTO card should be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;User registration.&lt;/li&gt;
&lt;li&gt;Linking PRESTO Card number with a user account.&lt;/li&gt;
&lt;li&gt;"Tap" functionality, which calculates the amount to be charged.&lt;/li&gt;
&lt;li&gt;The app should let the user check their current balance.&lt;/li&gt;
&lt;li&gt;Fare inspector verification to check if the current PRESTO has an active trip.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Non-functional Requirements
&lt;/h2&gt;

&lt;p&gt;The non-functional requirements for this system are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;High Availability: The system needs to be highly available. We are aiming for 99.5% availability duration.&lt;/li&gt;
&lt;li&gt;Scalability: The system should easily scale up during peak traffic hours (office commute time), holidays or during special events because the number of user registrations, card linking requests and taps will go up significantly during those times.&lt;/li&gt;
&lt;li&gt;Eventual Consistency can be tolerated because once the card is tapped, the local balance will be directly deducted but the balance can be updated later. We can have a periodic sync of the card balance with the server. Since only the first tap is considered in a two hour duration or two taps for GO and UP, we can update the data eventually in few minutes instead of instantaneously.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Back-of-the-envelope Estimates
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;We know that the number of taps in 2024 was 41 million. Considering an increase of 2 million people per year, we are looking at 51 million taps in 5 years. So the total number of taps in 5 years is 43 + 45 + 47 + 49 + 51 = 237 million, which is an average of 47.5 million tap requests, which is 47.5 * (10 ^ 6) requests per year.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This means the number of requests per second is ~55 requests per second.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Assume 100 new card registrations per day, which is a very small number per second.&lt;/li&gt;
&lt;li&gt;The number of new user registrations is lesser than that. So we can assume 60 requests per second to the server.&lt;/li&gt;
&lt;li&gt;Therefore, the number of servers needed is not a significant number.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Low-level Design
&lt;/h2&gt;

&lt;p&gt;APIs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;POST /api/user&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;POST /api/card/{cardNumber}/{userId}&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;POST /api/card/{cardNumber}/tap/{terminalId}&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;GET /api/card/{cardNumber}/balance&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;GET /api/card/{cardNumber}/inspector/{inspectorId}&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  High-level Design
&lt;/h2&gt;

&lt;p&gt;As stated earlier, the number of servers needed is not significant. But the number of servers needed could increase during peak hours or special events. Scaling up would mean adding new servers. This can happen during the times when the traffic is high. Since we need to dynamically change the number of servers based on the traffic, it would be good to add a load balancer.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxubvw97kjnnuu1yr9k3s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxubvw97kjnnuu1yr9k3s.png" alt="Image description" width="795" height="537"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The service layer can be broken into microservices, each handling a different aspect of the system such as the User service, Tap service, and the Card service. Since the scaling needs of each of these components is different, splitting them into microservices will make it easier to scale them up or down independently.&lt;/p&gt;

&lt;p&gt;Also, it would make sense to have these services deployed in multiple pods and also in different regions. This is so that if one server were to go down, a server in a different region could take over and serve the incoming requests.&lt;/p&gt;

&lt;p&gt;Since eventual consistency is what is needed, we can use a non-relational database such as MongoDB or Cassandra. This would ensure availability and scalability of data. This would also ensure that no data is lost. However, we need to add logic to calculate the balance separately. This would also keep the system simple.&lt;/p&gt;

</description>
      <category>systemdesign</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Leetcode 227 (Java): Basic Calculator II</title>
      <dc:creator>Phanindra Allamsetty</dc:creator>
      <pubDate>Fri, 02 Aug 2024 21:09:41 +0000</pubDate>
      <link>https://dev.to/phaniallamsetty/leetcode-227-java-basic-calculator-ii-4i8l</link>
      <guid>https://dev.to/phaniallamsetty/leetcode-227-java-basic-calculator-ii-4i8l</guid>
      <description>&lt;h2&gt;
  
  
  Problem:
&lt;/h2&gt;

&lt;p&gt;Given a string s which represents an expression, evaluate this expression and return its value. &lt;/p&gt;

&lt;h2&gt;
  
  
  Assumptions and General Information:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The integer division should truncate toward zero.&lt;/li&gt;
&lt;li&gt;You may assume that the given expression is always valid. All intermediate results will be in the range of [(-2^31), (2^31) - 1].&lt;/li&gt;
&lt;li&gt;No built-in functions are allowed.&lt;/li&gt;
&lt;li&gt;s represents a valid expression.&lt;/li&gt;
&lt;li&gt;All the integers in the expression are non-negative integers in the range [0, (2^31) - 1].&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Examples
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: s = "3+2*2"
Output: 7
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: s = "1 + 2 * 3 - 5 / 4"
Output: 6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: s = "2 * 35 - 7"
Output: 63
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Constraints
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;1 &amp;lt;= s.length &amp;lt;= 3 * (10 ^ 5).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;s consists of integers and operators ('+', '-', '*', '/') separated by some number of spaces.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The answer is guaranteed to fit in a 32-bit integer.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Brute Force
&lt;/h2&gt;

&lt;p&gt;A naive approach to solve this problem would be to iterate through the string and find the operator with the highest precedence (in the order '/', '*', '+', '-') as per the BODMAS principle and perform the operation on the number preceding and succeeding the operator. We need to remember that we need to look for the number until we hit another operator or the lower or upper bounds of the string. We then calculate the result and save it. We then create another string with this intermediate result and move forward to the next operator.&lt;/p&gt;

&lt;p&gt;Since we have 4 operators, we perform this 4 times. By the end of it we should have a string that can be typecasted to a string. We also try to find the numbers (instead of digits) on each side of the operator.The maximum time taken for this operation can also be n. Therefore, the time complexity will be O(n^2) and the space complexity includes creating a string for each of the iterations. Therefore, the space complexity of another O(n).&lt;/p&gt;

&lt;h2&gt;
  
  
  First Approach
&lt;/h2&gt;

&lt;p&gt;We can use a stack to evaluate these expressions. Think of it as only one operation that needs to be performed on numbers in a stack. This would be a O(n) solution. In order for us to achieve that, we should evaluate all the other operations so that only one low precedence operation is left.&lt;br&gt;
Let us assume addition as that operation.&lt;/p&gt;

&lt;p&gt;For example, "1 + 2 * 3 - 5/ 4" can be simplified to "1 + 6 - 1". This can also be written as "1 + 6 + (-1)". If we have 1, 6 and -1 in the stack, we can just return their sum.&lt;/p&gt;

&lt;p&gt;In order to implement this, we keep track of a current number and a current operator. When the current operator is '+', we just add the number to the stack. For '-', we add -1 times the number to the stack. For '*' and '/', we take the current top of the stack, perform the operation with the current number and put it back on to the stack. Towards the end, we will have a stack that contains elements that need to be added.&lt;/p&gt;

&lt;h2&gt;
  
  
  Code
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public int calculate(String s) {
    char[] strArray = s.toCharArray();
    int currentNumber = 0;
    char operator = '+';
    Stack&amp;lt;Integer&amp;gt; stack = new Stack&amp;lt;&amp;gt;();
    int currentTop = -1;
    int newTop = -1;

    for (int i = 0; i &amp;lt; strArray.length; i++) {
        char currentChar = strArray[i];
        if (Character.isDigit(currentChar)) {
            currentNumber = currentNumber * 10 + currentChar - '0';
        }

        if ((!Character.isDigit(currentChar) &amp;amp;&amp;amp; !Character.isWhitespace(currentChar)) || (i == strArray.length - 1)) {
            switch (operator) {
                case '+':
                    stack.push(currentNumber);
                    break;
                case '-':
                    stack.push(-1 * currentNumber);
                    break;
                case '*':
                    currentTop = stack.pop();
                    newTop = currentTop * currentNumber;
                    stack.push(newTop);
                    break;
                case '/':
                    currentTop = stack.pop();
                    newTop = currentTop / currentNumber;
                    stack.push(newTop);
                    break;
            }

            operator = currentChar;
            currentNumber = 0;
        }
    }

    int sum = 0;
    while (!stack.empty()) {
        sum += stack.pop();
    }

    return sum;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Optimized Approach
&lt;/h2&gt;

&lt;p&gt;The above solution with a stack has a time complexity of O(n). However, the space complexity is also O(n) because we need to save elements onto a stack and the size of the stack varies with the input.&lt;/p&gt;

&lt;p&gt;This problem can be solved in O(1) or constant space complexity. We will be making some changes to the above algorithm in order to avoid using the stack.&lt;/p&gt;

&lt;p&gt;For this, we will use an integer variable called &lt;em&gt;result&lt;/em&gt;, which will hold the intermediate result. Therefore, instead of saving elements to the stack, we just add them to result. However, there is an issue with '*' and '/', where we save the partial results to the stack. To avoid that, we will use another variable called &lt;em&gt;lastNumber&lt;/em&gt; to hold the partial result.&lt;/p&gt;

&lt;p&gt;Using these two variables, we can avoid using the stack therefore reduce the space complexity.&lt;/p&gt;

&lt;h2&gt;
  
  
  Code
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public int calculate(String s) {
    char[] strArray = str.toCharArray();
    int currentNumber = 0;
    char currentOperator = '+';
    int result = 0;
    int lastNumber = 0;

    for(int i = 0; i &amp;lt; strArray.length; i++) {
        char currentChar = strArray[i];

        if(isDigit(currentChar)) {
            currentNumber = currentNumber * 10 + currentChar - '0';
        }

        if(!isDigit(currentChar) &amp;amp;&amp;amp; !isWhitespace(currentChar) || i == strArray.length - 1) {
            switch(currentOperator) {
                case '+':
                    result += lastNumber;
                    lastNumber = currentNumber;
                    break;
                case '-':
                    result += lastNumber;
                    lastNumber = -1 * currentNumber;
                    break;
                case '*':
                    lastNumber = lastNumber * currentNumber;
                    break;
                case '/':
                    lastNumber = lastNumber / currentNumber;
                    break;
            }

            if(i == strArray.length - 1) {
                result += lastNumber;
            }

            currentNumber = 0;
            currentOperator = currentChar;
        }
    }
    return result;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Leetcode 143 (Java): Reorder Linked List</title>
      <dc:creator>Phanindra Allamsetty</dc:creator>
      <pubDate>Thu, 01 Aug 2024 21:29:30 +0000</pubDate>
      <link>https://dev.to/phaniallamsetty/leetcode-143-java-reorder-linked-list-3381</link>
      <guid>https://dev.to/phaniallamsetty/leetcode-143-java-reorder-linked-list-3381</guid>
      <description>&lt;h2&gt;
  
  
  Problem: Given the head of a singly linked list. The list can be represented as:
&lt;/h2&gt;

&lt;p&gt;L0 --&amp;gt; L1 --&amp;gt; L2 --&amp;gt; ... --&amp;gt; Ln-1 --&amp;gt; Ln&lt;/p&gt;

&lt;p&gt;Reorder the list to be of the following form:&lt;/p&gt;

&lt;p&gt;L0 --&amp;gt; Ln --&amp;gt; L1 --&amp;gt; Ln-1 --&amp;gt; ...&lt;/p&gt;

&lt;p&gt;You may not modify the values in the list's nodes. Only nodes themselves may be changed.&lt;/p&gt;

&lt;p&gt;Example: &lt;br&gt;
Input: (1) --&amp;gt; (2) --&amp;gt; (3) --&amp;gt; (4) --&amp;gt; null&lt;br&gt;
Output: (1) --&amp;gt; (4) --&amp;gt; (2) --&amp;gt; (3) --&amp;gt; null&lt;/p&gt;

&lt;p&gt;Example: &lt;br&gt;
Input: (1) --&amp;gt; (2) --&amp;gt; (3) --&amp;gt; (4) --&amp;gt; (5) --&amp;gt; null&lt;br&gt;
Output: (1) --&amp;gt; (5) --&amp;gt; (2) --&amp;gt; (4) --&amp;gt; (3) --&amp;gt; null&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The number of nodes in the list is in the range [1, 5 * 10^4]&lt;/li&gt;
&lt;li&gt;1 &amp;lt;= Node.val &amp;lt;= 1000&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;A brute force algorithm to solve this problem would be to initialize a current pointer, navigate to the end of the linked list for each current pointer, and place the last node of the linked list as the next pointer of the current pointer. However, the time complexity for this solution is O(n^2) because we will be iterating through the linked list n times. The space complexity, however, is O(1).&lt;/p&gt;

&lt;p&gt;A better solution would be to find the middle node of the linked list, reverse the second half of the linked list and then merge the first and second halves to the linked list.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
(1) --&amp;gt; (2) --&amp;gt; (3) --&amp;gt; (4) --&amp;gt; (5) --&amp;gt; (6) --&amp;gt; null&lt;/p&gt;

&lt;p&gt;Middle node:&lt;br&gt;
                        middle&lt;br&gt;
(1) --&amp;gt; (2) --&amp;gt; (3) --&amp;gt; (4) --&amp;gt; (5) --&amp;gt; (6) --&amp;gt; null&lt;/p&gt;

&lt;p&gt;To find the middle node, we declare two pointers: fast and slow and initialize them to head of the linked list. The slow node traverses the nodes one after the other. The fast node traverses the nodes by skipping one node in each iteration making it twice as fast as the slow now. Therefore, by the time the fast node reaches the end of the linked list, the slow node would have reached the middle node.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ListNode slow = head;
ListNode fast = head;

while(fast != null &amp;amp;&amp;amp; fast.next != null) {
  slow = slow.next;
  fast = fast.next.next;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Reverse the second half:&lt;br&gt;
                  next&lt;br&gt;
previous  (4) --&amp;gt; (5) --&amp;gt; (6) --&amp;gt; null&lt;br&gt;
          current&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                 next
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;previous &amp;lt;-- (4)     (5) --&amp;gt; (6) --&amp;gt; null&lt;br&gt;
             current&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;     previous        next
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;null &amp;lt;-- (4)     (5) --&amp;gt; (6) --&amp;gt; null&lt;br&gt;
                 current&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;             previous        next
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;null &amp;lt;-- (4) &amp;lt;-- (5)     (6) --&amp;gt; null&lt;br&gt;
                         current&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                     previous
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;null &amp;lt;-- (4) &amp;lt;-- (5) &amp;lt;-- (6)     null&lt;br&gt;
                                 current&lt;/p&gt;

&lt;p&gt;To reverse the second half of the linked list, we declare a current pointer and assign the slow pointer to it. Therefore, the current node points to the middle node of the linked list. We also assign two pointers, previous and next and assign them to null. previous will hold the current node when current moves on to the next pointer. This way we keep track of three pointers: previous, current and next.&lt;/p&gt;

&lt;p&gt;We traverse through the second half of the linked list through the current pointer until we reach the end. For each iteration, we assign the next pointer to the next of current. We then assign the next of the current to the previous pointer. This way we are flipping the direction of the next pointers. We then assign the current to previous and next to current. This way all three pointers move one node forward while changing the order of the current node.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ListNode previous = null;
ListNode current = slow;
ListNode next = null;

while(current != null) {
  next = current.next;
  current.next = previous;
  previous = current;
  current = next;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will make the previous node the second half in the reversed order.&lt;/p&gt;

&lt;p&gt;Merge both halves:&lt;br&gt;
(1) --&amp;gt; (6) --&amp;gt; (2) --&amp;gt; (5) --&amp;gt; (3) --&amp;gt; (4) --&amp;gt; null&lt;/p&gt;

&lt;p&gt;By now, we have the second half of the linked list reversed. We need to assume both halves as two different linked lists and merge them and adjust the null pointer at the end accordingly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ListNode first = head;
ListNode second = previous;
ListNode temp = head;

while(second.next != null) {
  temp = temp.next;
  first.next = second;
  second = second.next;
  first.next.next = temp;
  first = first.next.next;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Code
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public ListNode reorder(ListNode head) {
  if(head == null) {
    return head;
  }


  // Find the middle node
  ListNode slow = head;
  ListNode fast = head;

  while(fast != null &amp;amp;&amp;amp; fast.next != null) {
    slow = slow.next;
    fast = fast.next.next;
  } 

  // Reverse the second half of the linked list
  ListNode previous = null;
  ListNode current = slow;
  ListNode next = null;

  while(current != null) {
    next = current.next;
    current.next = previous;
    previous = current;
    current = next;
  }

  // Merge both halves
  ListNode first = head;
  ListNode second = previous;
  ListNode temp = head;

  while(second.next != null) {
    temp = temp.next;
    first.next = second;
    second = second.next;
    first.next.next = temp;
    first = first.next.next;
  }
  return head;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The time complexity of the above algorithm is O(n), whereas the space complexity if O(1).&lt;/p&gt;

</description>
      <category>buildinpublic</category>
      <category>dsa</category>
      <category>leetcode143</category>
      <category>blind75</category>
    </item>
  </channel>
</rss>
