Sort a linked list using insertion sort.

思路:用一个新链表来执行插入排序,将新的节点插入到正确的位置;

代码:

public ListNode insertionSortList(ListNode head){
        if (head==null||head.next==null)return head;
        ListNode temp=new ListNode(Integer.MIN_VALUE);
        ListNode now=temp;
        ListNode cur=head;
        while (cur!=null){
            now=temp;//每次都从第一个节点开始比较
            ListNode next=cur.next;//选取要排序的链表的下一个节点
            while (now.next!=null&&now.next.val<cur.val){//判断要选取插入的位置
                now=now.next;
            }
            cur.next=now.next;//插入节点
            now.next=cur;//插入节点
            cur=next;
        }

        return temp.next;//摒弃第一个自己new的节点
    }

 

### Java HashMap Value Usage and Characteristics In Java, `HashMap` allows storing key-value pairs where each unique key maps to a specific value. For demonstrating operations on the values within a `HashMap`, consider initializing such a map as follows: ```java Map<String, String> query = new HashMap<>(); ``` Values in a `HashMap` can be manipulated through various methods provided by the class. To insert or update an entry based on its key, use the `put()` method which also returns any previous value associated with that key[^1]: ```java // Adding entries into the HashMap query.put("key1", "value1"); String oldValue = query.put("key1", "newValue"); // Updates 'key1' from "value1" to "newValue" ``` Retrieving a value linked to a particular key employs the `get()` function. If no mapping exists for this key, it will return null. ```java String retrievedValue = query.get("key1"); // Retrieves "newValue" ``` To check whether there is a binding for a given key without retrieving the actual value, apply the `containsKey()` method instead of invoking `get()`. This avoids unnecessary object creation when only existence matters. ```java boolean hasKey = query.containsKey("key1"); // Returns true since "key1" was added earlier. ``` For iterating over all keys present inside the hashmap along with their corresponding values, one approach involves obtaining a set view via `entrySet()`. Then iterate using enhanced-for loops while accessing both components per iteration step. ```java for (Map.Entry<String, String> entry : query.entrySet()) { System.out.println(entry.getKey() + "=" + entry.getValue()); } ``` Removing mappings according to specified conditions utilizes either `remove(Object key)`—if targeting single removals—or more complex constructs like streams combined with filters for batch processing tasks involving multiple criteria checks against current contents stored under different keys simultaneously. ```java // Removing an individual item query.remove("key1"); // Batch removing items matching certain condition(s), here exemplified by stream API usage query.entrySet().stream() .filter(e -> e.getValue().equals("someCondition")) .forEachOrdered(e -> query.remove(e.getKey())); ``` Additionally, note that unlike collections implementing interfaces such as `List` or even other types of sets (`HashSet`, `TreeSet`), elements contained within a `HashMap` do not maintain insertion order nor sort themselves automatically because they rely upon hashing mechanisms rather than sequential positioning rules applied during construction phases[^2].
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值