LinkedList各種操作

2021-07-09 14:34:45 字數 2310 閱讀 7300

public boolean ispalindrome(listnode head)

listnode mid = findmiddle(head);

listnode sec = reverse(mid.next);

while(head != null && sec != null && head.val == sec.val)

return sec == null;

}

1、linkedlist節點結構

listnode

public class listnode

}

2、查詢中間節點方法

private listnode findmiddle(listnode head) 

return slow;

}

3. merge合併操作

public listnode merge(listnode head1,listnode head2)else

ite = ite.next;

} if(head1 != null)else

return dummy.next;

}

4、liskedlist的排序方法1

public listnode sortlist(listnode head)

listnode mid = findmiddle(head);

listnode right = sortlist(mid.next);

mid.next = null;

listnode left = sortlist(head);

return merge(left,right);

}

5、找到尾節點

public listnode gettail(listnode head)

while (head.next != null)

return head;

}

6.連線三條鍊錶

public listnode concat(listnode left,listnode middle,listnode right)

7、linkedlist排序方法2 快速排序

public listnode sortlist_quicksort(listnode head)

listnode mid = findmiddle(head);

listnode leftdummy = new listnode(0),lefttail = leftdummy;

listnode middledummy = new listnode(0),middletail = middledummy;

listnode rightdummy = new listnode(0), righttail = rightdummy;

while (head != null)else if(head.val > mid.val)else

head = head.next;

} lefttail.next = null;

righttail.next = null;

middletail.next = null;

listnode left = sortlist_quicksort(leftdummy.next);

listnode right = sortlist_quicksort(rightdummy.next);

return concat(left, middledummy.next,right);

}

8、反轉鍊錶

private listnode reverse(listnode head) 

return newhead;

}

9、判斷lingkedlist是否有環

public boolean hascycle(listnode head)else

head = head.next;

} return false;

}

10.刪除給定節點

public void deletenode(listnode node)

11.判斷是否為回文

LinkedList基本操作

一 概述 linkedlist與arraylist一樣實現list介面,只是arraylist是list介面的大小可變陣列的實現,linkedlist是list介面鍊錶的實現。基於鍊錶實現的方式使得linkedlist在插入和刪除時更優於arraylist,而隨機訪問則比arraylist遜色些。l...

redis 各種操作

1 查詢伺服器redis 配置檔案 find name redis.conf netstat tunpl grep 6379 檢視redis 預設埠號6379占用情況 2 bind 127.0.0.1 bind 192.168.1.100 繫結redis伺服器網絡卡ip,預設為127.0.0.1,即...

Django request各種操作

根據請求的資訊獲取base url 有時候服務的網域名稱比較多,還是需要動態的拼接一下url資訊 url request.get host wificdn.com 8888 request.get full path u wxpay qrcode2 16122010404238801544?name...