Search in sources :

Example 16 with ListNode

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

the class FebruaryW2 method reverseList.

/* https://leetcode.com/problems/reverse-linked-list/ */
public ListNode reverseList(ListNode head) {
    ListNode newHead = null;
    while (head != null) {
        ListNode next = head.next;
        head.next = newHead;
        newHead = head;
        head = next;
    }
    return newHead;
}
Also used : ListNode(com.pkumar7.datastructures.ListNode)

Example 17 with ListNode

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

the class SeptemberW1 method rotateRight.

/**
 * 61. Rotate List
 * https://leetcode.com/problems/rotate-list/
 *
 * Input: 1->2->3->4->5->NULL, k = 2
 * Output: 4->5->1->2->3->NULL
 */
public ListNode rotateRight(ListNode head, int k) {
    if (head == null || head.next == null)
        return head;
    ListNode dummy = new ListNode(0);
    dummy.next = head;
    ListNode slow = dummy, fast = dummy;
    int i;
    for (i = 0; fast.next != null; i++) {
        fast = fast.next;
    }
    int j;
    for (j = i - k % i; j > 0; j--) {
        slow = slow.next;
    }
    fast.next = dummy.next;
    dummy.next = slow.next;
    slow.next = null;
    return dummy.next;
}
Also used : ListNode(com.pkumar7.datastructures.ListNode)

Example 18 with ListNode

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

the class SeptemberW3 method oddEvenList.

/**
 * 328. Odd Even Linked List
 *
 * https://leetcode.com/problems/odd-even-linked-list/
 *
 * Input: 1->2->3->4->5->NULL
 * Output: 1->3->5->2->4->NULL
 */
public ListNode oddEvenList(ListNode head) {
    if (head == null)
        return head;
    ListNode odd = head, even = head.next, evenhead = even;
    while (even != null && even.next != null) {
        odd.next = even.next;
        odd = odd.next;
        even.next = odd.next;
        even = even.next;
    }
    odd.next = evenhead;
    return head;
}
Also used : ListNode(com.pkumar7.datastructures.ListNode)

Example 19 with ListNode

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

the class A method plusOne.

/* 369. Plus One Linked List
     * https://leetcode.com/problems/plus-one-linked-list/
     * */
public ListNode plusOne(ListNode head) {
    ListNode curr = dfs(head);
    if (carry == 1) {
        ListNode newHead = new ListNode(carry);
        newHead.next = curr;
        return newHead;
    }
    return curr;
}
Also used : ListNode(com.pkumar7.datastructures.ListNode)

Example 20 with ListNode

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

the class A method mergeInBetween.

/* 1669. Merge In Between Linked Lists
     * https://leetcode.com/problems/merge-in-between-linked-lists/
     * */
public ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) {
    ListNode prev_a = null, next_b = null;
    boolean a_found = false;
    ListNode temp = list1;
    while (temp != null) {
        if (!a_found && a-- == 0) {
            a_found = true;
        }
        if (b-- == 0) {
            next_b = temp.next;
            break;
        }
        if (!a_found) {
            prev_a = temp;
        }
        temp = temp.next;
    }
    if (prev_a == null) {
        list1 = list2;
    } else {
        prev_a.next = list2;
    }
    while (list2.next != null) {
        list2 = list2.next;
    }
    list2.next = next_b;
    return list1;
}
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