Search in sources :

Example 11 with ListNode

use of com.pkumar7.datastructures.ListNode in project Data-Structures-Algorithms by pankajgangwar.

the class NovemberW3 method swapRecursion.

public ListNode swapRecursion(ListNode head) {
    if (head == null || head.next == null)
        return head;
    ListNode remaining = head.next.next;
    ListNode newhead = head.next;
    head.next.next = head;
    head.next = swapRecursion(remaining);
    return newhead;
}
Also used : ListNode(com.pkumar7.datastructures.ListNode)

Example 12 with ListNode

use of com.pkumar7.datastructures.ListNode in project Data-Structures-Algorithms by pankajgangwar.

the class NovemberW3 method swapIterative.

public ListNode swapIterative(ListNode head) {
    ListNode dummy = new ListNode(0);
    dummy.next = head;
    ListNode point = dummy;
    ListNode swap1 = null;
    ListNode swap2 = null;
    while (point.next != null && point.next.next != null) {
        swap1 = point.next;
        swap2 = point.next.next;
        point.next = swap2;
        swap1.next = swap2.next;
        swap2.next = swap1;
        point = swap1;
    }
    return dummy.next;
}
Also used : ListNode(com.pkumar7.datastructures.ListNode)

Example 13 with ListNode

use of com.pkumar7.datastructures.ListNode in project Data-Structures-Algorithms by pankajgangwar.

the class NovemberW4 method reorderList.

/*  https://leetcode.com/problems/reorder-list/
        Given 1->2->3->4->5, reorder it to 1->5->2->4->3.
    */
public void reorderList(ListNode head) {
    if (head == null && head.next == null)
        return;
    ListNode slow = head;
    ListNode fast = head;
    ListNode prev = null;
    ListNode l1 = head;
    while (fast != null && fast.next != null) {
        prev = slow;
        slow = slow.next;
        fast = fast.next.next;
    }
    prev.next = null;
    ListNode l2 = reverse(slow);
    merge(l1, l2);
}
Also used : ListNode(com.pkumar7.datastructures.ListNode)

Example 14 with ListNode

use of com.pkumar7.datastructures.ListNode in project Data-Structures-Algorithms by pankajgangwar.

the class NovemberW4 method reverse.

public ListNode reverse(ListNode head) {
    ListNode prev = null;
    ListNode curr = head;
    ListNode next = null;
    while (curr != null) {
        next = curr.next;
        curr.next = prev;
        prev = curr;
        curr = next;
    }
    return prev;
}
Also used : ListNode(com.pkumar7.datastructures.ListNode)

Example 15 with ListNode

use of com.pkumar7.datastructures.ListNode in project Data-Structures-Algorithms by pankajgangwar.

the class NovemberW4 method merge.

public void merge(ListNode l1, ListNode l2) {
    ListNode next1 = null;
    ListNode next2 = null;
    while (l1 != null && l2 != null) {
        next1 = l1.next;
        next2 = l2.next;
        l1.next = l2;
        if (next1 == null)
            break;
        l2.next = next1;
        l1 = next1;
        l2 = next2;
    }
}
Also used : ListNode(com.pkumar7.datastructures.ListNode)

Aggregations

ListNode (com.pkumar7.datastructures.ListNode)29 TreeNode (com.pkumar7.TreeNode)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Stack (java.util.Stack)1