环形单链表的头节点 head 和 一个整数 num。
在给定的函数内返回新的环形单链表的头指针。
5 1 2 3 4 5 6
1 2 3 4 5 6
保证链表的长度不大于1000000
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; class ListNode { public int val; public ListNode next; public ListNode(int val) { this.val = val; this.next = null; } } public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine().trim()); String[] elems = br.readLine().trim().split(" "); // 构建单向循环链表 ListNode head = new ListNode(Integer.parseInt(elems[0])); ListNode cur = head; for(int i = 1; i < n; i++){ cur.next = new ListNode(Integer.parseInt(elems[i])); cur = cur.next; } cur.next = head; // 插入节点 int target = Integer.parseInt(br.readLine().trim()); head = insertNode(head, target, n); // 遍历插入节点后的链表 int cursor = 0; while(cursor < n + 1){ System.out.print(head.val + " "); head = head.next; cursor ++; } } private static ListNode insertNode(ListNode head, int target, int len) { ListNode cur = head; int cursor = 0; if(cur.val >= target){ // 如果头结点的值就比target大,就遍历到尾节点,改变尾节点指针的指向 while(cursor < len - 1){ cur = cur.next; cursor ++; } ListNode node = new ListNode(target); cur.next = node; node.next = head; return node; }else{ // 否则将target插入到合适的中间位置(也有可能是最后一个位置) while(cur.next.val < target && cursor < len - 1){ cur = cur.next; cursor ++; } ListNode temp = cur.next; ListNode node = new ListNode(target); node.next = temp; cur.next = node; return head; } } }
list_node * insert_num(list_node * head, int num) { //////在下面完成代码 list_node *node = new list_node; node -> val = num; list_node *p = head; while (p -> next != head) { p = p -> next; } if (head -> val >= num) { node -> next = head; p -> next = node; head = node; return head; } if (p -> val <= num) { p -> next = node; node -> next = head; return head; } p = head; while (p -> next != head) { if (p -> val <= num && p -> next -> val >= num) { node -> next = p -> next; p -> next = node; break; } p = p -> next; } return head; }
list_node * insert_num(list_node * head, int num) { //////在下面完成代码 list_node* node = new list_node; node->val = num; if(head==nullptr) { //空链表情况 node->next = node; return node; } list_node* p=head; if(head->val>num) { //头节点比插入值大的情况,需要把插入值作为新的头 while(p->next != head) p=p->next; p->next = node; node->next = head; return node; } if(head->next == head) { head->next = node; node->next = head; return head; } list_node* preNode = head; p = p->next; while(p!=head) { list_node* curNode=p; p = p->next; if(curNode->val <num) { preNode = curNode; continue; } preNode->next = node; node->next = curNode; break; } return head; }书上的代码如下:(更简洁)
list_node * insert_num(list_node * head, int num) { //////在下面完成代码 list_node* node = new list_node; node->val = num; if(head==nullptr) { node->next = node; return node; } list_node* pre = head; list_node* cur = head->next; while(cur!=head) { if(pre->val <= num && cur->val >=num) break; pre = cur; cur = cur->next; } pre->next = node; node->next = cur; return head->val<num?head:node; }
#列表元素个数 n=int(input()) #列表元素 number_list=list(map(int,input().split())) #要插入的元素 num=int(input()) number_list.append(num) #排序 number_list.sort() #输出 print(' '.join(map(str,number_list)))
list_node * insert_num(list_node * head, int num) { //////在下面完成代码 list_node * cur = head; list_node * new_node = new list_node(); list_node * pre; new_node->val= num; new_node->next= NULL; while(cur->val<=num&&cur->next!=head){ pre = cur; cur = cur->next; } if(cur->next==head){ //如果不小于tail值,则插入tail之后,返回head if(cur->val<=num){ new_node->next = cur->next; cur->next = new_node; return head; } //如果小于head值,则插入tail和head之间,并作为新head返回 if(num<head->val){ new_node->next = cur->next; cur->next = new_node; return new_node; } } //插入pre和cur之间,返回head new_node->next = cur; pre->next = new_node; return head; }
list_node * insert_num(list_node * head, int num) { list_node* pre=head; list_node* phead=head->next; list_node* p=head; list_node* pnode=new list_node; pnode->val=num; while(p->next!=head){ p=p->next; } if(head->val>num){ p->next=pnode; pnode->next=head; return pnode; } if(p->val<num){ p->next=pnode; pnode->next=head; return head; } while(phead->val<num) { pre = phead; phead = phead->next; if(phead == head) break; } pre->next = pnode; pnode->next =phead; return head; }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static class Node { public int value; public Node next; public Node(int value) { this.value = value; } } public static Node listGenerator(int length, String[] numbers) { Node head = new Node(Integer.parseInt(numbers[0])); Node cur = head; for (int i = 1; i < length; i++) { cur.next = new Node(Integer.parseInt(numbers[i])); cur = cur.next; } cur.next = null; return head; } public static Node insert(Node head, int num) { Node node = new Node(num); if (head == null) { node.next = node; return node; } Node pre = head; Node cur = head.next; while (cur != head) { if (pre.value <= num && cur.value >= num) { break; } pre = pre.next; cur = cur.next; } pre.next = node; node.next = cur; return head.value < node.value ? head : node; } public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(bufferedReader.readLine()); String[] numbers = bufferedReader.readLine().split(" "); int num = Integer.parseInt(bufferedReader.readLine()); Node head = listGenerator(n, numbers); Node node = head; while (node.next != null) { node = node.next; } node.next = head; head = insert(head, num); System.out.print(head.value + " "); node = head.next; while (node != head) { System.out.print(node.value + " "); node = node.next; } System.out.println(); } }
list_node * getlastnode(list_node * root) { list_node * p = root; while(p->next !=root) p = p->next; return p; } list_node * insert_num(list_node * head, int num) { //////在下面完成代码 //插入是正常插入 list_node * pnode = new list_node(); pnode->val=num; //保存头节点 list_node * phead = head; //还需要考虑头节点比要插入的节点大的情况,看了别的代码才想起来,这脑子 list_node * pre = getlastnode(head); if(head->val>=num) { pre->next = pnode; pnode->next=head; return pnode; } //开始遍历 while(phead->val<num) { pre = phead; phead = phead->next; if(phead == head) break; } //这时候就可以直接插入 pre->next = pnode; pnode->next =phead; return head; }