#26. Merge Two Sorted Lists
You are given the heads of two sorted linked lists list1 and list2. Merge the two lists in a one-sorted list. The list should be made by splicing together the nodes of the first two lists. Return the head of the merged linked list.
#27. Merge k Sorted Lists
You are given an array of k linked-lists lists, each linked list is sorted in ascending order. Merge all the linked lists into one sorted linked list and return it.
public ListNode mergeKLists_2_by_2(ListNode[] lists) {
#28. Remove Nth Node From End of List
Given the head of a linked list, remove the nth node from the end of the list and return its head.
/*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode() {}* ListNode(int val) { this.val = val; }* ListNode(int val, ListNode next) { this.val = val; this.next = next; }* }*/class Solution {public ListNode removeNthFromEnd(ListNode head, int n) {if (n <= 0) {return head; // n too small}ListNode p1 = head, p2 = head;for (int i=0; i<n; i++) { // p2 will get at distance n from p1if (p2 != null) {p2 = p2.next;} else {return head; // n too large}}if (p2 == null) {// n-th node from the end is headhead = p1.next;p1.next = null;return head;}// p1 and p2 go one step at a time until p2 gets to the last positionwhile (p2 != null && p2.next != null) {p1 = p1.next;p2 = p2.next;}// remove p1.next// if p2 was null, then we would have deleted p1 (but then no access to its previous node)ListNode deleted = p1.next;p1.next = p1.next.next;deleted.next = null;return head;}}
#29. Reorder List
You are given the head of a singly linked list. The list can be represented as:
L0 → L1 → … → Ln - 1 → Ln
Reorder the list to be on the following form:
L0 → Ln → L1 → Ln - 1 → L2 → Ln - 2 → …
You may not modify the values in the list's nodes. Only nodes themselves may be changed.
class Solution {private void swap(ListNode currentLeft, ListNode beforeCurrentRight) {ListNode next = currentLeft.next;currentLeft.next = beforeCurrentRight.next;currentLeft.next.next = next;beforeCurrentRight.next = null;}public void reorderList(ListNode head) { // no value swap; only full node swapif (head == null) {return;}ListNode p1 = head, p2 = head;Stack<ListNode> stack = new Stack<>();while (p2.next != null) {stack.push(p2);p2 = p2.next;}int halfSize = stack.size() / 2;if (stack.size() % 2 == 1) {halfSize++;}while (stack.size() > halfSize) {p2 = stack.pop();if (p1 == p2) {break;}swap(p1, p2);if (p1.next == null || p1.next.next == null) {break;}p1 = p1.next.next;}}}
Niciun comentariu:
Trimiteți un comentariu