Skip to content

Commit b939118

Browse files
committed
Chapter 05 section 03 C++ codes updated. Java codes added.
1 parent d3f5b64 commit b939118

File tree

5 files changed

+164
-35
lines changed

5 files changed

+164
-35
lines changed

05-About-Linked-List/Course Code (C++)/03-Remove-Linked-List-Elements/main.cpp

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ struct ListNode {
1212
/// LinkedList Test Helper Functions
1313
ListNode* createLinkedList(int arr[], int n){
1414

15-
if( n == 0 )
15+
if(n == 0)
1616
return NULL;
1717

1818
ListNode* head = new ListNode(arr[0]);
1919
ListNode* curNode = head;
20-
for( int i = 1 ; i < n ; i ++ ){
20+
for(int i = 1 ; i < n ; i ++){
2121
curNode->next = new ListNode(arr[i]);
2222
curNode = curNode->next;
2323
}
@@ -27,28 +27,28 @@ ListNode* createLinkedList(int arr[], int n){
2727

2828
void printLinkedList(ListNode* head){
2929

30-
if( head == NULL ){
31-
cout<<"NULL"<<endl;
30+
if(head == NULL){
31+
cout << "NULL" << endl;
3232
return;
3333
}
3434

3535
ListNode* curNode = head;
36-
while( curNode != NULL ){
37-
cout<<curNode->val;
38-
if( curNode->next != NULL )
39-
cout<<" -> ";
36+
while(curNode != NULL){
37+
cout << curNode->val;
38+
if(curNode->next != NULL)
39+
cout << " -> ";
4040
curNode = curNode->next;
4141
}
4242

43-
cout<<endl;
43+
cout << endl;
4444

4545
return;
4646
}
4747

4848
void deleteLinkedList(ListNode* head){
4949

5050
ListNode* curNode = head;
51-
while( curNode != NULL ){
51+
while(curNode != NULL){
5252
ListNode* delNode = curNode;
5353
curNode = curNode->next;
5454
delete delNode;
@@ -57,25 +57,28 @@ void deleteLinkedList(ListNode* head){
5757
return;
5858
}
5959

60+
// 203. Remove Linked List Elements
61+
// https://leetcode.com/problems/remove-linked-list-elements/description/
6062
// 不使用虚拟头结点
63+
// 时间复杂度: O(n)
64+
// 空间复杂度: O(1)
6165
class Solution {
6266
public:
6367
ListNode* removeElements(ListNode* head, int val) {
6468

65-
while( head != NULL && head->val == val ){
66-
69+
// 需要对头结点进行特殊处理
70+
while(head != NULL && head->val == val){
6771
ListNode* node = head;
6872
head = head->next;
6973
delete node;
7074
}
7175

72-
if( head == NULL )
76+
if(head == NULL)
7377
return head;
7478

7579
ListNode* cur = head;
76-
while( cur->next != NULL ){
77-
78-
if( cur->next->val == val ){
80+
while(cur->next != NULL){
81+
if(cur->next->val == val){
7982
ListNode* delNode = cur->next;
8083
cur->next = delNode->next;
8184
delete delNode;
@@ -91,15 +94,15 @@ class Solution {
9194
int main() {
9295

9396
int arr[] = {1, 2, 6, 3, 4, 5, 6};
94-
int n = sizeof(arr)/sizeof(int);
97+
int n = sizeof(arr) / sizeof(int);
9598

9699
ListNode* head = createLinkedList(arr, n);
97100
printLinkedList(head);
98101

99-
Solution().removeElements( head, 6);
102+
Solution().removeElements(head, 6);
100103
printLinkedList(head);
101104

102-
deleteLinkedList( head );
105+
deleteLinkedList(head);
103106

104107
return 0;
105108
}

05-About-Linked-List/Course Code (C++)/03-Remove-Linked-List-Elements/main2.cpp

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ struct ListNode {
1212
/// LinkedList Test Helper Functions
1313
ListNode* createLinkedList(int arr[], int n){
1414

15-
if( n == 0 )
15+
if(n == 0)
1616
return NULL;
1717

1818
ListNode* head = new ListNode(arr[0]);
1919
ListNode* curNode = head;
20-
for( int i = 1 ; i < n ; i ++ ){
20+
for(int i = 1 ; i < n ; i ++){
2121
curNode->next = new ListNode(arr[i]);
2222
curNode = curNode->next;
2323
}
@@ -27,28 +27,28 @@ ListNode* createLinkedList(int arr[], int n){
2727

2828
void printLinkedList(ListNode* head){
2929

30-
if( head == NULL ){
31-
cout<<"NULL"<<endl;
30+
if(head == NULL){
31+
cout << "NULL" << endl;
3232
return;
3333
}
3434

3535
ListNode* curNode = head;
36-
while( curNode != NULL ){
37-
cout<<curNode->val;
38-
if( curNode->next != NULL )
39-
cout<<" -> ";
36+
while(curNode != NULL){
37+
cout << curNode->val;
38+
if(curNode->next != NULL)
39+
cout << " -> ";
4040
curNode = curNode->next;
4141
}
4242

43-
cout<<endl;
43+
cout << endl;
4444

4545
return;
4646
}
4747

4848
void deleteLinkedList(ListNode* head){
4949

5050
ListNode* curNode = head;
51-
while( curNode != NULL ){
51+
while(curNode != NULL){
5252
ListNode* delNode = curNode;
5353
curNode = curNode->next;
5454
delete delNode;
@@ -57,18 +57,22 @@ void deleteLinkedList(ListNode* head){
5757
return;
5858
}
5959

60+
// 203. Remove Linked List Elements
61+
// https://leetcode.com/problems/remove-linked-list-elements/description/
6062
// 使用虚拟头结点
63+
// 时间复杂度: O(n)
64+
// 空间复杂度: O(1)
6165
class Solution {
6266
public:
6367
ListNode* removeElements(ListNode* head, int val) {
6468

69+
// 创建虚拟头结点
6570
ListNode* dummyHead = new ListNode(0);
6671
dummyHead->next = head;
6772

6873
ListNode* cur = dummyHead;
69-
while( cur->next != NULL ){
70-
71-
if( cur->next->val == val ){
74+
while(cur->next != NULL){
75+
if(cur->next->val == val){
7276
ListNode* delNode = cur->next;
7377
cur->next = delNode->next;
7478
delete delNode;
@@ -87,15 +91,15 @@ class Solution {
8791
int main() {
8892

8993
int arr[] = {1, 2, 6, 3, 4, 5, 6};
90-
int n = sizeof(arr)/sizeof(int);
94+
int n = sizeof(arr) / sizeof(int);
9195

9296
ListNode* head = createLinkedList(arr, n);
9397
printLinkedList(head);
9498

95-
Solution().removeElements( head, 6);
99+
Solution().removeElements(head, 6);
96100
printLinkedList(head);
97101

98-
deleteLinkedList( head );
102+
deleteLinkedList(head);
99103

100104
return 0;
101105
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Definition for singly-linked list.
2+
// 在Java版本中,我们将LinkedList相关的测试辅助函数写在ListNode里
3+
public class ListNode {
4+
5+
public int val;
6+
public ListNode next = null;
7+
8+
public ListNode(int x) {
9+
val = x;
10+
}
11+
12+
// 根据n个元素的数组arr创建一个链表
13+
// 使用arr为参数,创建另外一个ListNode的构造函数
14+
public ListNode (int[] arr){
15+
16+
if(arr == null || arr.length == 0)
17+
throw new IllegalArgumentException("arr can not be empty");
18+
19+
this.val = arr[0];
20+
ListNode curNode = this;
21+
for(int i = 1 ; i < arr.length ; i ++){
22+
curNode.next = new ListNode(arr[i]);
23+
curNode = curNode.next;
24+
}
25+
}
26+
27+
// 返回以当前ListNode为头结点的链表信息字符串
28+
@Override
29+
public String toString(){
30+
31+
StringBuilder s = new StringBuilder("");
32+
ListNode curNode = this;
33+
while(curNode != null){
34+
s.append(Integer.toString(curNode.val));
35+
s.append(" -> ");
36+
curNode = curNode.next;
37+
}
38+
s.append("NULL");
39+
return s.toString();
40+
}
41+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// 203. Remove Linked List Elements
2+
// https://leetcode.com/problems/remove-linked-list-elements/description/
3+
// 不使用虚拟头结点
4+
// 时间复杂度: O(n)
5+
// 空间复杂度: O(1)
6+
public class Solution1 {
7+
8+
public ListNode removeElements(ListNode head, int val) {
9+
10+
// 需要对头结点进行特殊处理
11+
while(head != null && head.val == val){
12+
ListNode node = head;
13+
head = head.next;
14+
}
15+
16+
if(head == null)
17+
return head;
18+
19+
ListNode cur = head;
20+
while(cur.next != null){
21+
if(cur.next.val == val){
22+
ListNode delNode = cur.next;
23+
cur.next = delNode.next;
24+
}
25+
else
26+
cur = cur.next;
27+
}
28+
29+
return head;
30+
}
31+
32+
public static void main(String[] args) {
33+
34+
int[] arr = {1, 2, 6, 3, 4, 5, 6};
35+
int val = 6;
36+
37+
ListNode head = new ListNode(arr);
38+
System.out.println(head);
39+
40+
(new Solution1()).removeElements(head, val);
41+
System.out.println(head);
42+
}
43+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// 203. Remove Linked List Elements
2+
// https://leetcode.com/problems/remove-linked-list-elements/description/
3+
// 使用虚拟头结点
4+
// 时间复杂度: O(n)
5+
// 空间复杂度: O(1)
6+
public class Solution2 {
7+
8+
public ListNode removeElements(ListNode head, int val) {
9+
10+
// 创建虚拟头结点
11+
ListNode dummyHead = new ListNode(0);
12+
dummyHead.next = head;
13+
14+
ListNode cur = dummyHead;
15+
while(cur.next != null){
16+
if(cur.next.val == val ){
17+
ListNode delNode = cur.next;
18+
cur.next = delNode.next;
19+
}
20+
else
21+
cur = cur.next;
22+
}
23+
24+
return dummyHead.next;
25+
}
26+
27+
public static void main(String[] args) {
28+
29+
int[] arr = {1, 2, 6, 3, 4, 5, 6};
30+
int val = 6;
31+
32+
ListNode head = new ListNode(arr);
33+
System.out.println(head);
34+
35+
(new Solution1()).removeElements(head, val);
36+
System.out.println(head);
37+
}
38+
}

0 commit comments

Comments
 (0)