JAVA - Data structure- Singly linked list

  * Definition for singly-linked list.

 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Main{


       ListNode headNode = new ListNode();  //new a LstNode Object
        ListNode nextNode = headNode;
       
        for(int value : Something){
            nextNode.next = new ListNode(value);
            nextNode = nextNode.next;
        }
        
        headNode=headNode.next;
        return headNode;   //<<     

    }
}

留言

此網誌的熱門文章

MAP - Sort with stream

JAVA - DSF Example