ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Day7] 876. Middle of the Linked List / 86. Partition List (LeetCode)
    @StudY/.algorithm 2019. 2. 22. 14:12

    빛나는 졸업장을 타느라 어제는 회사도 알고리즘 공부도 하루 휴식!


    876. Middle of the Linked List Easy

    /**

     * Definition for singly-linked list.

     * public class ListNode {

     *     int val;

     *     ListNode next;

     *     ListNode(int x) { val = x; }

     * }

     */

    class Solution {

        public ListNode middleNode(ListNode head) {

            ListNode middle = head;

            ListNode counter = head;

            int count=1;

            

            while(counter.next!=null) {

                count++;

                counter = counter.next;

            }

            

            for (int i=0; i<count/2; i++) {

                    middle = middle.next;

            }

            

            return middle;

        }

    }


    Accepted0 ms36.8 MBjava




    /**

     * Definition for singly-linked list.

     * public class ListNode {

     *     int val;

     *     ListNode next;

     *     ListNode(int x) { val = x; }

     * }

     */

    class Solution {

        public ListNode middleNode(ListNode head) {

            ListNode middle = head;

            int count=1;

            

            while(head.next!=null) {

                count++;

                head = head.next;

            }

            

            for (int i=0; i<count/2; i++) {

                    middle = middle.next;

            }

            

            return middle;

        }

    }


    Accepted1 ms36.7 MBjava

    86. Partition List Medium

    /**

     * Definition for singly-linked list.

     * public class ListNode {

     *     int val;

     *     ListNode next;

     *     ListNode(int x) { val = x; }

     * }

     */

    class Solution {

        public ListNode partition(ListNode head, int x) {

            if (head==null) return head;

            

            ListNode result, iter, tail, temp;

            result = new ListNode(0);

            tail = new ListNode(0);

            iter = result;

            iter.next = tail;

            

            while (head.next != null) {

                if (head.val < x) {

                    temp = iter.next;

                    iter.next = head;

                    head = head.next;

                    iter.next.next = temp;

                    iter = iter.next;

                } else {

                    temp = tail.next;

                    tail.next = head;

                    head = head.next;

                    tail.next.next = temp;

                    tail = tail.next;

                }

            }

            

            if (head.val < x) {

                temp = iter.next;

                iter.next = head;

                head = head.next;

                iter.next.next = temp;

                iter = iter.next;

            } else {

                temp = tail.next;

                tail.next = head;

                head = head.next;

                tail.next.next = temp;

                tail = tail.next;

            }

            

            iter.next = iter.next.next;

            return result.next;

        }

    }


    Accepted0 ms36.8 MBjava




    댓글

Designed by Tistory.