leet-codes.blogspot.com Open in urlscan Pro
2a00:1450:4001:811::2001  Public Scan

URL: https://leet-codes.blogspot.com/2022/12/odd-even-linked-list.html
Submission: On October 06 via api from US — Scanned from DE

Form analysis 1 forms found in the DOM

https://leet-codes.blogspot.com/search

<form action="https://leet-codes.blogspot.com/search" target="_top">
  <div class="search-input">
    <input aria-label="Search this blog" autocomplete="off" name="q" placeholder="Search this blog" value="">
  </div>
  <label>
    <input type="submit">
    <svg class="svg-icon-24 touch-icon search-icon">
      <use xlink:href="/responsive/sprite_v1_6.css.svg#ic_search_black_24dp" xmlns:xlink="http://www.w3.org/1999/xlink"></use>
    </svg>
  </label>
</form>

Text Content

Skip to main content
Search


SEARCH THIS BLOG





LEETCODE SOLUTIONS | PLATFORM FOR LEARNING TO CODE

Leet-codes solution is the best place to learn data structures, algorithms, and
most asked coding interview questions free of cost.



Share
 * Get link
 * Facebook
 * Twitter
 * Pinterest
 * Email
 * Other Apps

Labels
 * linked-list
 * two-pointer

December 06, 2022


328. ODD EVEN LINKED LIST


ODD EVEN THE LINKED LIST

Problem Description



Given the head of a singly linked list, group all the nodes with odd indices
together followed by the nodes with even indices, and return the reordered list.

The first node is considered odd, and the second node is even, and so on.

Note that the relative order inside both the even and odd groups should remain
as it was in the input.

You must solve the problem in O(1) extra space complexity and O(n) time
complexity.

 

Example 1:




Input: head = [1,2,3,4,5]
Output: [1,3,5,2,4]



Example 2:




Input: head = [2,1,3,5,6,4,7]
Output: [2,3,6,7,1,5,4]


 

Constraints:

 * The number of nodes in the linked list is in the range [0, 104].
 * -106 <= Node.val <= 106



Approach) Brute Force 



var oddEvenList = function(head) {
    if (!head) return head;
    
    var odd = head,
        even = head.next;
    // console.log({head, odd, even})
    while (odd && odd.next) {
        
        // tmp equal to memory address of even (head.next), 
       // so even will move too. 
       // Can't use even straightly because in the end we will use it.
        var tmp = odd.next;
        // console.log('begin: ', {head, even})
        
        // handle the odd to remove value of even order
        odd.next = odd.next.next;  
        // console.log('middle: ', {head, even})
        
        if ( odd.next !== null ) {
            // move odd forward to next
            odd = odd.next;
            
            // handle the even to remove value of odd order 
            tmp.next = odd.next;  
        }
        // console.log('end: ', {head, even})
    }
    
    // point the end of odd to the begin of the even
    odd.next = even;
    // console.log({head, odd, even})  
    
    return head;
};



Approach) Two Pointers



var oddEvenList = function(head) {
    if (!head) return null;
    let odd = head;
    let even = head.next;
    let evenHead = even;

    while (even && even.next) {
        odd.next = even.next;
        odd = odd.next;
        even.next = odd.next;
        even = even.next;
    }

    odd.next = evenHead;
    return head;
};







Conclusion



That’s all folks! In this post, we solved LeetCode problem 328. Odd Even the
Linked List

I hope you have enjoyed this post. Feel free to share your thoughts on this.

You can find the complete source code on my GitHub repository. If you like what
you learn. feel free to fork 🔪 and star ⭐ it.


In this blog, I have tried to collect & present the most important points to
consider when improving Data structure and logic, feel free to add, edit,
comment, or ask. For more information please reach me here
Happy coding!

Share
 * Get link
 * Facebook
 * Twitter
 * Pinterest
 * Email
 * Other Apps

Labels: linked-list two-pointer



COMMENTS



POST A COMMENT

Powered by Blogger


TECH-ENTHUSIAST | JAVASCRIPT | FULLSTACK FEEL FREE TO ADD, EDIT, COMMENT, OR ASK

For more information


ARCHIVE

 * September 20231
 * December 202231
 * November 202255
 * October 202257
 * September 202254


LABELS

 * 2DArray4
 * Array77
 * backtrack15
 * bash4
 * binary-search15
 * binary-search-tree2
 * binary-tree12
 * bit-manipulation6
 * breadth-first-search13
 * bucket-sorting2

 * Cache1
 * counting2
 * data-stream2
 * Database8
 * depth-first-search30
 * design4
 * DFS1
 * divide-and-conquer5
 * doubly-linked-list1
 * Dynamic programming4
 * dynamic-programming28
 * graph1
 * greedy10
 * Hashmap11
 * Hashtable27
 * heap4
 * heap-priority-queue3
 * in1
 * kmp1
 * left-join1
 * Linked List1
 * linked-list15
 * Map1
 * math26
 * Matrix17
 * memo1
 * merge-sort1
 * mysql2
 * ordered-set1
 * Pattern1
 * priority-queue2
 * Query1
 * queue3
 * Recursion26
 * script4
 * segment-tree2
 * set7
 * shell4
 * simulation2
 * sliding-window8
 * slow-fast-pointer3
 * sorting19
 * SQL6
 * Stack19
 * String45
 * subquery2
 * swap1
 * tree13
 * trie2
 * Two Pointer2
 * two-pointer20

Show more Show less


PAGEVIEWS

34,596
Diese Website verwendet Cookies von Google, um Dienste anzubieten und Zugriffe
zu analysieren. Deine IP-Adresse und dein User-Agent werden zusammen mit
Messwerten zur Leistung und Sicherheit für Google freigegeben. So können
Nutzungsstatistiken generiert, Missbrauchsfälle erkannt und behoben und die
Qualität des Dienstes gewährleistet werden.Weitere InformationenOk