a1b2c level
获赞
0
粉丝
0
关注
0
看过 TA
0
中南大学
2027
算法工程师
IP属地:陕西
暂未填写个人简介
私信
关注
import java.util.Scanner;public class demo {public static void main(String[] args) {//移除链表元素//构造链表1-->4-->2-->4Scanner sc = new Scanner(System.in);int n = sc.nextInt();//链表共有节点个数sc.nextLine();//构造单链表  尾插法ListNode head = null;//head一旦确定,就不再移动ListNode tail = null;//随着新节点的加入,不断向后移动if (n > 0){for (int i = 1; i <= n; i++){int val = sc.nextInt();//输入链表ListNode newNode = new ListNode(val);if (head == null){//插入第一个节点时,head既是头又是尾head = newNode;tail = head;}else{tail.next = newNode;tail = tail.next;}}}sc.nextLine();int target = sc.nextInt();//需要移除的目标值//如果头节点本身就要删除while (head != null && head.val == target){head = head.next;//直接将head后移}//判断是否为空if (head == null){return;}//处理头节点之后的节点ListNode current = head;while (current.next != null){if (current.next.val == target){//找到目标,则移除current.next = current.next.next;}else {//没找到,继续向后current = current.next;}}while (head != null){System.out.print(head.val + " ");head = head.next;}}}class ListNode{int val;ListNode next;ListNode(int val){this.val = val;}}
0 点赞 评论 收藏
分享
import java.util.Scanner;public class demo {public static void main(String[] args) {//螺旋矩阵:给出一个正整数n,按从外向内的螺旋顺序打印 1-n^2 的所有数// 1 2 3// 8 9 4// 7 6 5//注:坚持循环不变量原则(左开右闭或左闭右开)Scanner sc = new Scanner(System.in);int n = sc.nextInt();int[][] res = new int[n][n];//定义边界int top = 0,bottom = n - 1;int left = 0,right = n - 1;int num = 1;//初始填充数字int target = n * n;//终止数字while (num <= target) {//1.从左到右填充上边界for (int i = left; i <= right; i++) {res[top][i] = num++;}top++;//上边界下移//2.从上到下填充右边界for (int i = top; i <= bottom; i++) {res[i][right] = num++;}right--;//右边界左移//3.从右到左填充下边界for (int i = right; i >= left; i--) {res[bottom][i] = num++;}bottom--;//下边界上移//4.从下到上填充左边界for (int i = bottom; i >= top; i--) {res[i][left] = num++;}left++;//左边界右移}for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {System.out.print(res[i][j] + "  ");}System.out.println();}}}
0 点赞 评论 收藏
分享
0 点赞 评论 收藏
分享

创作者周榜

更多
关注他的用户也关注了:
牛客网
牛客网在线编程
牛客网题解
牛客企业服务