不同語言中節點結構的定義 I

鏈表節點

C++
1
2
3
4
5
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
C#
1
2
3
4
5
6
7
8
public class ListNode {
public int val;
public ListNode next;
public ListNode(int x) {
val = x;
next = null;
}
}
Java
1
2
3
4
5
6
7
8
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
function ListNode(val) {
this.val = val;
this.next = null;
}
// Or in TypeScript ...
class ListNode {
val: number
next: ListNode | null
constructor(val?: number, next?: ListNode | null) {
this.val = (val===undefined ? 0 : val)
this.next = (next===undefined ? null : next)
}
}
Python
1
2
3
4
class ListNode:
def __init__(self, x):
self.val = x
self.next = None