Implement SLList.insert which takes in an integer x and an integer position. It
inserts x at the given position. If position is after the end of the list, insert the
new node at the end.
For example, if the SLList is 5 → 6 → 2, insert(10, 1) results in 5 → 10 → 6 → 2
and if the SLList is 5 → 6 → 2, insert(10, 7) results in 5 → 6 → 2 → 10.
Additionally, for this problem assume that position is a non-negative integer.
First to construct .
idea: 首先我们需要分类讨论,先讨论特殊情况,首先是position=0 或者first=null的情况,这样的最简单,因为就是相当于直接add first就可以,因为本来就在首位,不需要调换指针,也不需要将节点接到上一个节点。 然后便是正常的情况,position超过原元素个数的后面再讨论。
public void insert(int item, int position){
if(position == 0||first = null