示例#1
0
		public static void ReorderList(ListNode head){
			if (head == null || head.next == null) //handle edge case
				return;

			//split the list use fast and slow pointers
			ListNode fast = head;
			ListNode slow = head;
			while (fast.next != null && fast.next.next != null) {
				fast = fast.next.next;
				slow = slow.next;
			}
			ListNode second = slow.next;
			slow.next = null; //final step for splitting list, cut two lists off

			//reverse second list
			second = ReverseList (second);

			ListNode first = head;
			while (first != null && second != null) {
				ListNode temp1 = first.next;
				ListNode temp2 = second.next;
				second.next = first.next;
				first.next = second;
				first = temp1;
				second = temp2;
			}
		}
示例#2
0
		private static ListNode ReverseList(ListNode head){ //reverse the list 
			if (head == null || head.next == null) //handle edge case
				return head;

			ListNode current = head;
			ListNode next = head.next;
			while (next != null) {
				ListNode temp = next.next;
				next.next = current;
				current = next;
				next = temp;
			}
			head.next = null; //set original head.next to null
			return current;

		}
示例#3
0
		public ListNode(int v){
			this.val = v;
			this.next = null;
		}