Day 3

Remove Linked List Elements

Using a dummy node.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public ListNode removeElements(ListNode head, int val) {
ListNode dummy = new ListNode();
dummy.next = head;
ListNode cur = dummy;

while (cur.next != null) {
if (cur.next.val == val) {
cur.next = cur.next.next;
} else {
cur = cur.next;
}
}
return dummy.next;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
func removeElements(head *ListNode, val int) *ListNode {
dummy := &ListNode{}
dummy.Next = head
cur := dummy

for cur.Next != nil {
if cur.Next.Val == val {
cur.Next = cur.Next.Next
} else {
cur = cur.Next
}
}
return dummy.Next
}

Design Linked List

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
class MyLinkedList {
int size;
ListNode head;
ListNode tail;

public MyLinkedList() {
size = 0;
head = new ListNode(0);
tail = new ListNode(0);
head.next = tail;
tail.prev = head;
}

public int get(int index) {
if (index < 0 || index >= size) {
return -1;
}
ListNode curr;

if (index + 1 < size / 2) {
curr = head;
for (int i = 0; i <= index; i++) {
curr = curr.next;
}
} else {
curr = tail;
for (int i = 0; i < size - index; i++) {
curr = curr.prev;
}
}
return curr.val;
}

public void addAtHead(int val) {
this.addAtIndex(0, val);
}

public void addAtTail(int val) {
this.addAtIndex(size, val);
}

public void addAtIndex(int index, int val) {
if (index > size) {
return;
}
index = index < 0 ? 0 : index;
ListNode pred, succ;
if (index < size / 2) {
pred = head;
for (int i = 0; i < index; i++) {
pred = pred.next;
}
succ = pred.next;
} else {
succ = tail;
for (int i = 0; i < size - index; i++) {
succ = succ.prev;
}
pred = succ.prev;
}
size++;
ListNode newNode = new ListNode(val);
newNode.prev = pred;
newNode.next = succ;
pred.next = newNode;
succ.prev = newNode;
}

public void deleteAtIndex(int index) {
if (index >= size || index < 0) {
return;
}
ListNode pred, succ;
if (index < size / 2) {
pred = head;
for (int i = 0; i < index; i++) {
pred = pred.next;
}
succ = pred.next.next;
} else {
succ = tail;
for (int i = 0; i < size - index - 1; i++) {
succ = succ.prev;
}
pred = succ.prev.prev;
}
size--;

pred.next = succ;
succ.prev = pred;

}
}

class ListNode {
int val;
ListNode next;
ListNode prev;

public ListNode(int val) {
this.val = val;
}
}

Reverse Linked List

So eeeezzzzz !!!

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public ListNode reverseList(ListNode head) {
ListNode prev = null;

while (head != null) {
ListNode next = head.next;
head.next = prev;
prev = head;
head = next;
}
return prev;
}
}
1
2
3
4
5
6
7
8
9
10
11
func reverseList(head *ListNode) *ListNode {
var prev *ListNode

for head != nil {
next := head.Next
head.Next = prev
prev = head
head = next
}
return prev
}