Repository navigation
Expand file tree
/
Copy pathQuestions.java
More file actions
118 lines (94 loc) · 2.6 KB
/
Copy pathQuestions.java
File metadata and controls
118 lines (94 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
LC: 142. Linked List Cycle II
Difficulty: Medium
Pattern: Linked List + Two Pointers
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode slow = head;
ListNode fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {
slow = head;
while (slow != fast) {
slow = slow.next;
fast = fast.next;
}
return slow;
}
}
return null;
}
}
*/
/*
LC: 25. Reverse Nodes in k-Group
Difficulty: Hard
Pattern: Linked List + Recursion
class Solution {
public ListNode reverseKGroup(ListNode head, int k){
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode prevGroup = dummy;
while (prevGroup.next != null) {
ListNode first = prevGroup.next;
ListNode temp = first;
for (int i = 1; i < k; i++){
temp = temp.next;
if (temp == null){
return dummy.next;
}
}
ListNode nextGroup = temp.next;
ListNode prev = nextGroup;
ListNode current = first;
for (int i = 0; i < k; i++){
ListNode next = current.next;
current.next = prev;
prev = current;
current = next;
}
prevGroup.next = temp;
prevGroup = first;
}
return dummy.next;
}
}
*/
/*
LC: 143.Reorder List
Difficulty: Medium
Pattern: Linked List + Two Pointers
class Solution {
public void reorderList(ListNode head) {
if(head == null || head.next == null){
return;
}
ListNode slow = head;
ListNode fast = head;
while(fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
}
ListNode prev = null;
ListNode current = slow;
while(current != null){
ListNode next = current.next;
current.next = prev;
prev = current;
current = next;
}
ListNode first = head;
ListNode second = prev;
while(second.next != null){
ListNode FirstText = first.next;
ListNode SecondText = second.next;
first.next = second;
second.next = FirstText;
first = FirstText;
second = SecondText;
}
}
}
*/