Day 48

Remove Nth Node From End of List

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode();
dummy.next = head;
ListNode fast = dummy;
ListNode slow = dummy;

for (int i = 0; i < n; i++) {
fast = fast.next;
}

while (fast.next != null) {
fast = fast.next;
slow = slow.next;
}
slow.next = slow.next.next;
return dummy.next;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
func removeNthFromEnd(head *ListNode, n int) *ListNode {
dummy := &ListNode{
Val: 0,
Next: head,
}
slow, fast := dummy, dummy

for i := 0; i < n; i++ {
fast = fast.Next
}

for fast.Next != nil {
fast = fast.Next
slow = slow.Next
}

slow.Next = slow.Next.Next
return dummy.Next
}

Intersection of Two Linked Lists

1
2
3
4
5
6
7
8
9
10
11
12
13
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode p1 = headA;
ListNode p2 = headB;

while (p1 != p2) {
p1 = p1 == null ? headB : p1.next;
p2 = p2 == null ? headA : p2.next;
}

return p1;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
func getIntersectionNode(headA, headB *ListNode) *ListNode {
p1, p2 := headA, headB

for p1 != p2 {
if p1 != nil {
p1 = p1.Next
} else {
p1 = headB
}

if p2 != nil {
p2 = p2.Next
} else {
p2 = headA
}
}
return p1
}

Linked List Cycle II

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode fast = head;
ListNode slow = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
if (slow == fast) {
ListNode p1 = fast;
ListNode p2 = head;

while(p1 != p2) {
p1 = p1.next;
p2 = p2.next;
}
return p1;
}
}
return null;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
func detectCycle(head *ListNode) *ListNode {
slow, fast := head, head
for fast != nil && fast.Next != nil {
fast = fast.Next.Next
slow = slow.Next
if slow == fast {
p1, p2 := fast, head
for p1 != p2 {
p1 = p1.Next
p2 = p2.Next
}
return p1
}
}
return nil
}