Linked-list operation(java)

   Here I didn't wanna list the operations about Linked-list but just share some thinking when I dealt with the problems about linked-list.

   When I dealt with those problems, I felt so confused about the operation.

   But after I solved a few problems in leetcode. I figured out a most important idea. In order to explain the idea, I need set a example. If we have a list whose first node is head.The question is, what dose "head.next" means?

   Actually, it depends on  the relative position. As we all known, the left side of the equation is associated with address, the right side is the value. The operation of Linked-list is the same idea: If it is left side, "head.next=anotherNode" means head's next node has been changed to anotherNode; If it is right side, "anotherNode=head.next" means assign head's next node to anotherNode.

  One is position, another is value.

  For example, head list is "1-->2-->3-->4-->null". If ListNode anotherNode=new ListNode(9);

If executing  "head.next=anotherNode": head list is "1-->9";

If executing  "anotherNode=head.next": head list will not be changed, but the anotherNode was changed as "2";


Tips: 

     1.When you visit the address of head.next, you must make sure head is not null and head has the next Node.Especially where the while or for cycle.

     2.When you try to add, delete, change the list, the main idea is to understand the relative position. Sometimes you must store the node in temporarily, otherwise we will lose the way to locate it. 

你可能感兴趣的:(Linked-list operation(java))