1. 程式人生 > >菜鳥的算法入門:java的鏈表操作

菜鳥的算法入門:java的鏈表操作

給定 color 第一次 即將 != 輸出 運行 原因 clas

從C語言的指針開始,我的算法之路就結束了!

今天為了找個好的實習,不得不撿起來,寫了三年的web,算法落下了太多了

今天在leetcode上刷題,難在了一個簡單的鏈表上,因此記錄一下

題目:給定兩個非空鏈表來表示兩個非負整數。位數按照逆序方式存儲,它們的每個節點只存儲單個數字。將兩數相加返回一個新的鏈表。
   你可以假設除了數字 0 之外,這兩個數字都不會以零開頭。
示例: 輸入:(
2 -> 4 -> 3) + (5 -> 6 -> 4) 輸出:7 -> 0 -> 8 原因:342 + 465 = 807

解題過程是幾經波折的,最開始弄出來的答案還是頭插式的,並且還超時了,真菜

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int flag=0;//用於判斷是否進位  1為進位,0為不進,可以直接做數值存儲
     int sum=0; //用於存放當前位相加的和
int i1=0;  //l1的val int i2=0;  //l2的val ListNode head=new ListNode(0); //聲明一個節點為頭節點,因為是尾插,最終的node為最後一個節點的節點值 ListNode node=head;  //聲明一個臨時變量,用於尾插的操作 while(l1!=null||l2!=null){
       //如果當前節點為空,則賦值為0,方便繼續運算
il = (l1 !=null) ? l1.val : 0;if(l2!=null){ i2
=l2.val; }else{ i2=0; } sum=i1+i2+flag; //得到當前運算的和 flag=sum/10;  //是否進位

       //對當前節點尾插一個節點,存儲當前節點的值 第一次運算時,相當於head.next=new ListNode(7) 這也是為什麽最後返回head.next的原因 node.next
=new ListNode(sum%10);
       //將當前節點的next賦值給當前節點,即將指針移到鏈表的尾部 node
=node.next; if (l1 != null) l1 = l1.next; if (l2 != null) l2 = l2.next; }
     //如果最後又進位,再給尾部插入一個新的節點
if (flag > 0) { node.next = new ListNode(flag); } return head.next; } }

在本題中,采用的是尾插法,不停的在鏈表的尾部插入新的節點

取值時,只需要對最開始的head進行向下取值即可

輸入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
輸出:7 -> 0 -> 8
原因:342 + 465 = 807

0.開始循環之前

head={
    val : 0
    next : null
}
node={
    val : 0
    next : null
}

1.第一次循環

node.next=new ListNode(sum%10); 
運行結果:
node={
    val : 0
    next : {
        val : 7
        next : null
    }
}
由於head=node:
head={
    val : 0
    next : {
        val : 7
        next : null
    }
}
node=node.next;
運行結果:
node={
    val : 7
    next : null;
}

head不變

2.第二次循環

node.next=new ListNode(sum%10); 
運行結果:
node={
    val : 7
    next : {
        val : 0
        next : null
    }
}
由於head=node:
head={
    val : 0
    next : {
        val : 7
        next : {
            val : 0
            next : null
        }
    }
}
node=node.next;
運行結果:
node={
    val : 0
    next : null;
}

head不變    

3.第三次循環

node.next=new ListNode(sum%10); 
運行結果:
node={
    val : 0
    next : {
        val : 8
        next : null
    }
}
由於head=node:
head={
    val : 0
    next : {
        val : 7
        next : {
            val : 0
            next : {
                val : 8
                next : null
            }
        }
    }
}
node=node.next;
運行結果:
node={
    val : 8
    next : null;
}

head不變          

4.返回結果

head={
    val : 0
    next : {
        val : 7
        next : {
            val : 0
            next : {
                val : 8
                next : null
            }
        }
    }
}

head.next={
    val : 7
    next : {
        val : 0
        next : {
            val : 8
            next : null
        }
    }
} 

5.總結

最開始的head節點是整個運算中不動的,node節點相當於其的一個尾巴,
不斷的添加數據到自身,然後後移到添加的節點上去

類似於一個貪吃蛇,head節點一直不變,node節點是一個工作節點(長度為1)
  他的工作是找到一個新的節點,讓其附著在自己的next上(node.next=new ListNode)
  然後移到到這個新的節點上去(node=node.next)
  重復這項工作

菜鳥的算法入門:java的鏈表操作