Search in sources :

Example 1 with ListNode

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

the class A method insert.

ListNode insert(ListNode root, int item) {
    ListNode temp = new ListNode(item);
    ListNode ptr;
    temp.next = null;
    if (root == null)
        root = temp;
    else {
        ptr = root;
        while (ptr.next != null) ptr = ptr.next;
        ptr.next = temp;
    }
    return root;
}
Also used : ListNode(com.pkumar7.datastructures.ListNode)

Example 2 with ListNode

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

the class A method reverseNodes.

private ListNode reverseNodes(int len, ListNode head) {
    ListNode fastPtr = head;
    int i = 0;
    for (; i < len && fastPtr != null; i++) {
        fastPtr = fastPtr.next;
    }
    while (i-- > 0) {
        ListNode next = head.next;
        head.next = fastPtr;
        fastPtr = head;
        head = next;
    }
    head = fastPtr;
    return head;
}
Also used : ListNode(com.pkumar7.datastructures.ListNode)

Example 3 with ListNode

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

the class A method reverseEvenLengthGroups.

/*
    * https://leetcode.com/problems/reverse-nodes-in-even-length-groups/
    * 2074. Reverse Nodes in Even Length Groups
    * */
public ListNode reverseEvenLengthGroups(ListNode head) {
    int currLen = 1;
    ListNode temp = head;
    ListNode prev = head;
    int remainingNodes = findLength(head);
    while (temp != null) {
        if (currLen % 2 == 0 && remainingNodes >= currLen) {
            prev.next = reverseNodes(currLen, temp);
            temp = prev.next;
            int advance = currLen;
            while (advance-- > 0 && temp != null) {
                prev = temp;
                temp = temp.next;
                remainingNodes -= 1;
            }
        } else {
            if (currLen > remainingNodes && remainingNodes % 2 == 0) {
                prev.next = reverseNodes(currLen, temp);
                temp = prev.next;
            }
            int advance = currLen;
            while (advance-- > 0 && temp != null) {
                prev = temp;
                temp = temp.next;
                remainingNodes -= 1;
            }
        }
        currLen += 1;
    }
    return head;
}
Also used : ListNode(com.pkumar7.datastructures.ListNode)

Example 4 with ListNode

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

the class A method reverseBetween.

/* 92. Reverse Linked List II
    * https://leetcode.com/problems/reverse-linked-list-ii/
    * */
public ListNode reverseBetween(ListNode head, int left, int right) {
    if (left == right)
        return head;
    ListNode lNode = null, rNode = null, prevLNode = null;
    ListNode temp = head;
    int pos = 0;
    while (temp != null) {
        ++pos;
        if (pos == left) {
            lNode = temp;
        } else if (pos == right) {
            rNode = temp.next;
            temp.next = null;
        }
        if (lNode == null) {
            prevLNode = temp;
        }
        temp = temp.next;
    }
    ListNode newHead = null;
    ListNode lNodeTemp = lNode;
    while (lNode != null) {
        ListNode next = lNode.next;
        lNode.next = newHead;
        newHead = lNode;
        lNode = next;
    }
    if (lNodeTemp == null)
        return head;
    lNodeTemp.next = rNode;
    if (prevLNode != null) {
        prevLNode.next = newHead;
    } else {
        return newHead;
    }
    return head;
}
Also used : ListNode(com.pkumar7.datastructures.ListNode)

Example 5 with ListNode

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

the class AugustW4 method removeSubElements.

void removeSubElements(ListNode head) {
    ListNode start = head;
    ListNode end;
    while (start != null) {
        boolean mod = false;
        int sum = 0;
        end = start;
        while (end != null) {
            sum += end.val;
            if (sum == 0) {
                start = end;
                mod = true;
                break;
            }
            end = end.next;
        }
        if (!mod) {
            if (curr == null) {
                res.next = new ListNode(start.val);
                curr = res.next;
            } else {
                ListNode newNode = new ListNode(start.val);
                curr.next = newNode;
                curr = curr.next;
            }
        }
        start = start.next;
    }
}
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