From 8066d81c58b867bfa73d7a8067a1282dbc8ccdb6 Mon Sep 17 00:00:00 2001 From: Chen Xu Date: Fri, 19 Aug 2022 03:10:01 -0400 Subject: [PATCH] Day 3 - Linked List --- LinkedList.java | 133 ++++++++++++++++++++++++++++++++++++++++++++ utils/ListNode.java | 10 ++++ 2 files changed, 143 insertions(+) create mode 100644 LinkedList.java create mode 100644 utils/ListNode.java diff --git a/LinkedList.java b/LinkedList.java new file mode 100644 index 0000000..d4d2f29 --- /dev/null +++ b/LinkedList.java @@ -0,0 +1,133 @@ +import utils.ListNode; + +public class LinkedList { + public static void main(String[] args) { + } + + /** + * Reverse Linked List (iterative) + *

+ * Reverse a singly-linked list iteratively. + */ + public ListNode reverse(ListNode head) { + if (head == null || head.next == null) { + return head; + } + ListNode cur = head; + ListNode prev = null; + + while (cur != null) { + ListNode next = cur.next; + cur.next = prev; + prev = cur; + cur = next; + } + return prev; + } + + /** + * Reverse Linked List (recursive) + *

+ * Reverse a singly-linked list recursively. + */ + public ListNode reverse2(ListNode head) { + if (head == null || head.next == null) { + return head; + } + + ListNode newHead = reverse2(head); + head.next.next = head; + head.next = null; + + return newHead; + } + + /** + * Middle Node Of Linked List + *

+ */ + public ListNode middleNode(ListNode head) { + if (head == null || head.next == null) { + return head; + } + + ListNode fast = head; + while (fast.next != null && fast.next.next != null) { + fast = fast.next.next; + head = head.next; + } + return head; + } + + /** + * Check If Linked List Has A Cycle + *

+ * Check if a given linked list has a cycle. Return true if it does, otherwise + * return false. + */ + public boolean hasCycle(ListNode head) { + if (head == null) { + return false; + } + + ListNode fast = head; + while (fast.next != null && fast.next.next != null) { + fast = fast.next.next; + head = head.next; + if (fast == head) { + return true; + } + } + return true; + } + + /** + * Insert In Sorted Linked List + *

+ * Insert a value in a sorted linked list. + */ + public ListNode insert(ListNode head, int value) { + if (head == null) { + return new ListNode(value); + } + ListNode dummy = new ListNode(0); + dummy.next = head; + ListNode cur = dummy; + while (cur.next != null && cur.next.value < value) { + cur = cur.next; + } + ListNode node = new ListNode(value); + node.next = cur.next; + cur.next = node; + + return dummy.next; + } + + /** + * Merge Two Sorted Linked Lists + *

+ * Merge two sorted lists into one large sorted list. + */ + public ListNode merge(ListNode one, ListNode two) { + ListNode dummy = new ListNode(0); + ListNode cur = dummy; + while (one != null && two != null) { + if (one.value < two.value) { + cur.next = one; + one = one.next; + } else { + cur.next = two; + two = two.next; + } + cur = cur.next; + } + + if (one != null) { + cur.next = one; + } else { + cur.next = two; + } + + return dummy.next; + } +} diff --git a/utils/ListNode.java b/utils/ListNode.java new file mode 100644 index 0000000..f344f03 --- /dev/null +++ b/utils/ListNode.java @@ -0,0 +1,10 @@ +package utils; + +public class ListNode { + public int value; + public ListNode next; + + public ListNode(int value) { + this.value = value; + } +}