[0707.设计链表]
class MyLinkedNode{
int val;
MyLinkedNode next;
public MyLinkedNode (int val, MyLinkedNode next){
val = this.val;
next = this.next;
}
}
class MyLinkedList {
MyLinkedNode head;
public MyLinkedList() {
}
}
- 开始弄了半天,竟然不知道leecode给出的这个MyLinkedList是链表(而不是链表结点),题目让写的是链表操作函数,因此我还需要先自己定义链表结点类MyLinkedNode,先生成结点》》》》然后才能初始化链表》》》》接着才有对链表的操作函数的自定义
class MyLinkedNode{
int val;
MyLinkedNode next;
public MyLinkedNode (int val, MyLinkedNode next){
val = this.val;
next = this.next;
}
}
class MyLinkedList {
MyLinkedNode node;
MyLinkedList shead;
public MyLinkedList() {
MyLinkedNode node = new MyLinkedNode (-1 , null);
this.shead = node;
}
}
- 首先,要注意C、C++、Java在同时自定义两个类、并其中一个会用到另一个类的情况下,各类的属性、构造函数初始化的写法有很大区别。详见代码。
- 其次,对什么类定义-----就想这个类需要什么属性------而该属性对应什么类,按照这个思路就不会混淆了。(1)对结点定义---结点内的元素值、下一个结点地址值-----分别是int型、结点MyLinkedNode型(2)对链表定义---链表头指针(假的)、链表大小-------分别是MyLinkedNode型、int型。 上述代码我的错误是没考虑到链表大小。
- 最后,关于类中的属性核构造函数的理解:(1)属性格式:类型 变量名;是不需要在这个地方赋值的。(2)构造函数:是对各个属性赋值(即初始化)
public int get(int index) {
MyLinkedNode pre = this.shead;
MyLinkedNode cur = pre.next;
while(cur != null){
if(size == index){
return cur.val;
}
pre = cur;
cur = cur.next;
size++;
}
if(cur == null){
return -1;
}
}
- 上面代码不需要pre指针,删去
class MyLinkedNode{
int val;
MyLinkedNode next;
public MyLinkedNode (int val, MyLinkedNode next){
val = this.val;
next = this.next;
}
}
class MyLinkedList {
MyLinkedNode shead; //指向头结点的指针
int size; // 所有元素(含虚拟头结点)的下标从0-size ,总元素(不含虚拟头结点)共size个
public MyLinkedList() {
shead = new MyLinkedNode (-1, null);
size = 0;
}
public int get(int index) {
MyLinkedNode cur = shead.next;
while(cur != null){
if(size == index){
return cur.val;
}
cur = cur.next;
size++;
}
return -1;
}
public void addAtHead(int val) {
MyLinkedNode head = new MyLinkedNode(val , shead.next);
shead.next = head;
size ++;
}
public void addAtTail(int val) {
MyLinkedNode tail = new MyLinkedNode(val , null);
MyLinkedNode cur = shead;
for (int i =1; i <= size; i++){
cur = cur.next;
}
cur.next = tail;
size ++;
}
public void addAtIndex(int index, int val) { // index题目要求从0-index ,虚拟头结点是没有index值的
if (index == size){
MyLinkedNode tail = new MyLinkedNode(val , null);
MyLinkedNode cur = shead;
for (int i =1; i <= size; i++){
cur = cur.next;
}
cur.next = tail;
}
if (index < 0){
MyLinkedNode head = new MyLinkedNode(val , shead.next);
shead.next = head;
}
if (index >= 0 && index < size){
MyLinkedNode p = new MyLinkedNode(val , null);
MyLinkedNode cur = shead;
MyLinkedNode pre = cur;
for (int i = 0; i <= index; i++){
pre = cur;
cur = cur.next;
}
p.next = cur;
pre = p;
}
size ++;
}
public void deleteAtIndex(int index) {
MyLinkedNode cur = shead;
MyLinkedNode pre = cur;
if (index >= 0 && index < size){
for (int i = 0; i <= index; i++){
pre = cur;
cur = cur.next;
}
pre.next = cur.next;
}
size --;
}
}
/**
* Your MyLinkedList object will be instantiated and called as such:
* MyLinkedList obj = new MyLinkedList();
* int param_1 = obj.get(index);
* obj.addAtHead(val);
* obj.addAtTail(val);
* obj.addAtIndex(index,val);
* obj.deleteAtIndex(index);
*/
- 搞清了属性核构造函数,结果还是有问题
class MyLinkedNode{
int val;
MyLinkedNode next;
public MyLinkedNode (int val, MyLinkedNode next){
val = this.val;
next = this.next;
}
}
class MyLinkedList {
MyLinkedNode shead; //指向头结点的指针
int size; // 所有元素(含虚拟头结点)的下标从0-size ,总元素(不含虚拟头结点)共size个
public MyLinkedList() {
shead = new MyLinkedNode (-1, null);
size = 0;
}
public int get(int index) {
if (index < 0 || index >= size){
return -1;
}
MyLinkedNode cur = shead;
for (int i = 0; i <= index; i++ ){
cur = cur.next;
}
return cur.val;
}
public void addAtHead(int val) {
addAtIndex (0 , val);
}
public void addAtTail(int val) {
addAtIndex (size , val);
}
public void addAtIndex(int index, int val) { // index题目要求从0-index ,虚拟头结点是没有index值的
if (index > size)
return ;
if (index < 0){
index = 0;
}
if (index == size){
MyLinkedNode cur = shead;
MyLinkedNode p = new MyLinkedNode (val , null);
for (int i = 0; i <size; i++){
cur = cur.next;
}
cur.next = p;
size ++;
}
if (index >= 0 && index < size){
MyLinkedNode cur = shead;
MyLinkedNode pre = cur;
MyLinkedNode p = new MyLinkedNode (val , null);
for (int i = 0; i <= index; i++ ){
pre = cur;
cur = cur.next;
}
pre.next = p;
p.next = cur;
size++;
}
}
public void deleteAtIndex(int index) {
MyLinkedNode cur = shead;
MyLinkedNode pre = cur;
if (index < 0 || index >= size) {
return;
}
if (index >= 0 && index < size){
for (int i = 0; i <= index; i++){
pre = cur;
cur = cur.next;
}
pre.next = cur;
}
size --;
}
}
/**
* Your MyLinkedList object will be instantiated and called as such:
* MyLinkedList obj = new MyLinkedList();
* int param_1 = obj.get(index);
* obj.addAtHead(val);
* obj.addAtTail(val);
* obj.addAtIndex(index,val);
* obj.deleteAtIndex(index);
*/
- 结果还是不对,我估计是插入函数出问题了
class MyLinkedNode{
int val;
MyLinkedNode next;
MyLinkedNode(){}
MyLinkedNode (int val){
val = this.val;
}
}
class MyLinkedList {
MyLinkedNode shead; //指向头结点的指针
int size; //
public MyLinkedList() {
shead = new MyLinkedNode (0);
size = 0;
}
public int get(int index) {
if (index < 0 || index >= size){
return -1;
}
MyLinkedNode cur = shead;
for (int i = 0; i <= index; i++ ){
cur = cur.next;
}
return cur.val;
}
public void addAtHead(int val) {
addAtIndex (0 , val);
}
public void addAtTail(int val) {
addAtIndex (size , val);
}
public void addAtIndex(int index, int val) { // index题目要求从0-index ,虚拟头结点是没有index值的
if (index > size)
return;
if (index < 0){
index = 0;
}
size++;
//找到要插入节点的前驱
MyLinkedNode pred = shead;
for (int i = 0; i < index; i++) {
pred = pred.next;
}
MyLinkedNode toAdd = new MyLinkedNode(val);
toAdd.next = pred.next;
pred.next = toAdd;
}
public void deleteAtIndex(int index) {
if (index < 0 || index >= size) {
return;
}
size--;
if (index == 0) {
shead = shead.next;
return;
}
MyLinkedNode pred = shead;
for (int i = 0; i < index ; i++) {
pred = pred.next;
}
pred.next = pred.next.next;
}
}
/**
* Your MyLinkedList object will be instantiated and called as such:
* MyLinkedList obj = new MyLinkedList();
* int param_1 = obj.get(index);
* obj.addAtHead(val);
* obj.addAtTail(val);
* obj.addAtIndex(index,val);
* obj.deleteAtIndex(index);
*/
-
和标准答案一致但结果还是那两个数不对
-
[null,null,null,null,0,null,0] 答案:[null,null,null,null,2,null,3]
-
我天!!!!是
this.val = val;
我前后给写反了!!!>-< -
上上一段代码确实插入函数有问题