亚信的实习的一到笔试题,交换链表的后半部分
来源:互联网 发布:网络直播项目计划书 编辑:程序博客网 时间:2024/04/29 16:10
链表节点结构
package parameterValues;public class LNode {int value;LNode next;}
这是主程序
package parameterValues;import org.junit.Test;import sun.security.action.GetLongAction;public class LinkListParameter {/** * 交换链表后半部分的值,如果链表长度为偶数 * 则交换N/2个数 * 若链表长度为奇数 * 则交换(N+1)/2个数 */ public static void main(String[] args) {int a[] = {1,2,3,4,5,6};LNode head = initLNode(a);print(head);head = swapLinkList(head);print(head);} private static LNode swapLinkList(LNode head) {int length = 0;LNode t = head;while( t != null){t = t.next;length++;}int mid = 0;if(length % 2 == 0){mid = length /2;}else{mid = (length+1)/2;}LNode newhead = new LNode();LNode nhead = newhead;newhead.value = head.value;newhead.next = head.next;System.out.println("mid ="+mid);int l=1;while(l<mid){LNode cur = new LNode();cur.value = getLNodeByIndex(head, l).value;cur.next = null;newhead.next = cur;newhead = cur;l++;}l = length-1;while(l>=mid && l<length){LNode cur = new LNode();cur.value = getLNodeByIndex(head,l).value;cur.next = null;newhead.next = cur;newhead = cur;l--;}print(nhead);return nhead;}private static LNode getLNodeByIndex(LNode head, int index) {LNode node = head;int x=0;while( x < index ){if(node.next == null ) return null;node = node.next;x++;}return node;}@Testpublic void getLNodeByIndexTest(){int a[] = {1,2,3,4,8,5,6,8,9};LNode head = initLNode(a);System.out.println(getLNodeByIndex(head, 7).value);}private static LNode initLNode(int[] a) { LNode list = new LNode(); LNode head; list.value = a[0]; list.next = null; head = list; for (int i = 1; i < a.length; i++) {LNode cur = new LNode();cur.value = a[i];list.next = cur;list = cur;} return head;}public static void print(LNode nd){ System.out.println("print out LNode list"); while(nd != null){ System.out.print(nd.value+" "); nd = nd.next; } System.out.println(); }}
0 0
- 亚信的实习的一到笔试题,交换链表的后半部分
- 亚信广州的笔试
- 在亚信实习的日子
- 链表后半部分反转(2016亚信实习生笔试题)
- 在亚信的新生活
- 亚信java笔试题
- 亚信科技笔试题
- 一个实习的笔试题
- 亚信实习日记
- 亚信实习记录。
- 阿里实习笔试速度复习(一) ---其实考试考到的很少
- 亚信java笔试题[转载]
- 亚信面试笔试~~
- 2012腾讯实习笔试中看到的Catalan数
- 2012腾讯实习笔试中看到的Catalan数
- 2012腾讯实习笔试中看到的Catalan数
- 针对近期实习笔试题的看法
- 实习后的交流
- android view手势冲突的通用解决方法
- hdu 1114 Piggy-Bank(完全背包)
- Java进阶笔记(装饰者模式)
- Quartz.NET c# 教程 - 课程四:Triggers
- HDU 1847 Good Luck in CET-4 Everybody! (巴什博弈)
- 亚信的实习的一到笔试题,交换链表的后半部分
- BZOJ3439 KPM的MC密码
- shell脚本的基础
- 让老百姓能坐上专车,让马路不堵车
- 应用程序安装卸载监听-BroadcastRecevier
- 镜花水月,过不留痕————铭记那些给我们带来进步的C语言小难题
- 多出的字符加省略号
- MVC的简单层级结构
- APP开发实战26-HTTP消息