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

來源:互聯網
上載者:User

標籤:樣本   while   自身   flag   除了   菜鳥   div   add   刷題   

從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的鏈表操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.