Search in sources :

Example 16 with ListNode

use of com.fishercoder.common.classes.ListNode in project Leetcode by fishercoder1534.

the class _725Test method test2.

@Test
public void test2() {
    root = LinkedListUtils.contructLinkedList(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
    k = 3;
    actual = solution1.splitListToParts(root, k);
    for (ListNode head : actual) {
        ListNode.printList(head);
    }
}
Also used : ListNode(com.fishercoder.common.classes.ListNode) Test(org.junit.Test)

Example 17 with ListNode

use of com.fishercoder.common.classes.ListNode in project Leetcode by fishercoder1534.

the class _725Test method test4.

@Test
public void test4() {
    root = LinkedListUtils.contructLinkedList(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
    k = 3;
    actual = solution2.splitListToParts(root, k);
    for (ListNode head : actual) {
        ListNode.printList(head);
    }
}
Also used : ListNode(com.fishercoder.common.classes.ListNode) Test(org.junit.Test)

Example 18 with ListNode

use of com.fishercoder.common.classes.ListNode in project Leetcode by fishercoder1534.

the class _23 method mergeKLists.

public ListNode mergeKLists(ListNode[] lists) {
    PriorityQueue<ListNode> heap = new PriorityQueue(new Comparator<ListNode>() {

        @Override
        public int compare(ListNode o1, ListNode o2) {
            return o1.val - o2.val;
        }
    });
    for (ListNode node : lists) {
        if (node != null) {
            heap.offer(node);
        }
    }
    ListNode pre = new ListNode(-1);
    ListNode temp = pre;
    while (!heap.isEmpty()) {
        ListNode curr = heap.poll();
        temp.next = new ListNode(curr.val);
        if (curr.next != null) {
            heap.offer(curr.next);
        }
        temp = temp.next;
    }
    return pre.next;
}
Also used : ListNode(com.fishercoder.common.classes.ListNode) PriorityQueue(java.util.PriorityQueue)

Example 19 with ListNode

use of com.fishercoder.common.classes.ListNode in project Leetcode by fishercoder1534.

the class _24 method swapPairs.

public ListNode swapPairs(ListNode head) {
    if (head == null || head.next == null) {
        return head;
    }
    ListNode second = head.next;
    ListNode third = second.next;
    second.next = head;
    head.next = swapPairs(third);
    return second;
}
Also used : ListNode(com.fishercoder.common.classes.ListNode)

Example 20 with ListNode

use of com.fishercoder.common.classes.ListNode in project Leetcode by fishercoder1534.

the class _328 method oddEvenList.

public ListNode oddEvenList(ListNode head) {
    if (head != null) {
        ListNode odd = head;
        ListNode even = head.next;
        ListNode evenHead = even;
        while (even != null && even.next != null) {
            odd.next = odd.next.next;
            even.next = even.next.next;
            odd = odd.next;
            even = even.next;
        }
        odd.next = evenHead;
    }
    return head;
}
Also used : ListNode(com.fishercoder.common.classes.ListNode)

Aggregations

ListNode (com.fishercoder.common.classes.ListNode)26 Test (org.junit.Test)10 ArrayDeque (java.util.ArrayDeque)1 ArrayList (java.util.ArrayList)1 PriorityQueue (java.util.PriorityQueue)1