数据结构—单链表
1、單鏈表定義
屬性包括:當前節點的值、下個鏈表節點、構造函數
public class ListNode {int val;ListNode next;ListNode(int x) { val = x; } }2、鏈表遍歷
class Solution {public int getDecimalValue(ListNode head) {while(head!=null){head = head.next;}return 0;} }例題:計算所有元素的和、將二進制鏈表轉為整數、將十進制鏈表轉為整數
class Solution {public int getDecimalValue(ListNode head) {int binary= 0;int decimal = 0;int sum = 0;while(head!=null){decimal res*10+head.val;binary= res*2+head.val;sum += head.valhead = head.next;}return sum;} }3、刪除指定節點
給定待刪除的節點
邏輯:待刪除節點的值=下個節點的值,待刪除節點指向下下個節點
class Solution {public void deleteNode(ListNode node) {node.val = node.next.val;node.next = node.next.next;} } 超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
- 上一篇: docker中创建redis及在外部使用
- 下一篇: java之Arrays工具类的使用