Cookie Consent by Free Privacy Policy Generator 📌 Partition List | LeetCode | Java


✅ Partition List | LeetCode | Java


💡 Newskategorie: Programmierung
🔗 Quelle: dev.to

Algorithm

  1. Initialize two dummy nodes: lessHead and highHead to serve as heads for two separate lists, one for nodes with values less than x and one for nodes with values greater than or equal to x.
  2. Initialize pointers less and high to keep track of the tail of the two lists respectively.
  3. Iterate through the original linked list (head) using a temporary pointer temp.
  4. For each node in the original list:
    • If the value of the current node (temp.val) is less than x, append it to the less list.
    • If the value of the current node is greater than or equal to x, append it to the high list.
  5. Connect the tail of the less list to the head of the high list.
  6. Return the head of the less list, which now contains the partitioned linked list.

Code

class Solution {
    public ListNode partition(ListNode head, int x) {

        ListNode lessHead = new ListNode(0);
        ListNode highHead = new ListNode(0);

        ListNode less = lessHead, high = highHead;

        ListNode temp = head;

        while(temp!=null){
            if(temp.val<x){
                less.next = new ListNode(temp.val);
                less = less.next;
            }
            else{
                high.next = new ListNode(temp.val);
                high = high.next;
            }
            temp = temp.next;
        }

        less.next = highHead.next;

        return lessHead.next;
    }
}

Thanks for reading🥰.
Feel free to comment🖌️ and like the post💓
Follow for more 🤝 && Happy Coding🚀👩‍💻

Don't forget to check-out my other socials😍:
Github
Hashnode
Medium
Twitter(X)

...

✅ Partition List | LeetCode | Java


📈 42.29 Punkte

✅ Flatten Binary Tree to Linked List | LeetCode | Java


📈 29.36 Punkte

✅ How to Initialize a Java List – List of String Initialization in Java


📈 27.22 Punkte

✅ Is there an easy way to copy the content of a linux partition into another linux partition


📈 25.85 Punkte

✅ KDE partition manager deleted windows partition and copied efi boot contents into it


📈 25.85 Punkte

✅ Can I install Gentoo inside Arch with my /boot partition in the Arch partition


📈 25.85 Punkte

✅ EaseUS Partition Master: Partition management software review


📈 25.85 Punkte

✅ Want to seed torrents downloaded in Windows partition from Linux Mint Partition


📈 25.85 Punkte

✅ [LeetCode] Visualization of Reverse Linked List


📈 23.31 Punkte

✅ Leetcode Day3 Linked List


📈 23.31 Punkte

✅ LeetCode Meditations: Reorder List


📈 23.31 Punkte

✅ LeetCode Meditations: Reverse Linked List


📈 23.31 Punkte

✅ File List Export 2.8.8 - Export folder contents to a list (was File list to Excel).


📈 22.67 Punkte

✅ List Within a List in Python – How to Initialize a Nested List


📈 22.67 Punkte

✅ Find First and Last Position of Element in Sorted Array | LeetCode | Java


📈 21.81 Punkte

✅ LeetCode's Defanging an IP Address - Java Solution Beats 100% Memory & 92% Runtime


📈 21.81 Punkte

✅ Successful Pairs of Spells and Potions | LeetCode | Java


📈 21.81 Punkte

✅ Find Peak Element | LeetCode | Java


📈 21.81 Punkte

✅ Maximum Average Subarray I | LeetCode | Java


📈 21.81 Punkte

✅ Remove All Adjacent Duplicates In String | LeetCode | Java


📈 21.81 Punkte

✅ Count Good Nodes in Binary Tree | LeetCode | Java


📈 21.81 Punkte

✅ Populating Next Right Pointers in Each Node | LeetCode | Java


📈 21.81 Punkte

✅ Maximum Number of Vowels in a Substring of Given Length | LeetCode | Java


📈 21.81 Punkte

✅ Evaluate Reverse Polish Notation | LeetCode | Java


📈 21.81 Punkte

✅ Path Sum | LeetCode | Java


📈 21.81 Punkte

✅ Valid Parentheses | LeetCode | Java


📈 21.81 Punkte

✅ Check if the Sentence Is Pangram | LeetCode | Java


📈 21.81 Punkte

✅ Search a 2D Matrix | LeetCode | Java


📈 21.81 Punkte

✅ Largest Number | LeetCode | Java


📈 21.81 Punkte

✅ Univalued Binary Tree | LeetCode | Java


📈 21.81 Punkte

✅ Merge In Between Linked Lists | LeetCode | Java


📈 21.81 Punkte











matomo

Datei nicht gefunden!