LeetCode_24---Swap Nodes in Pairs

Given a linked list, swap every two adjacent nodes and return its head.

For example,
Given 1->2->3->4, you should return the list as 2->1->4->3.

Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.

Hide Tags
  Linked List
翻译:


Code:



package From21;

import From21.LeetCode23.ListNode;

/**
 * @author MohnSnow
 * @time 2015年6月8日 下午5:25:18
 * 
 */
public class LeetCode24 {

	/**
	 * @param argsmengdx
	 *            -fnst
	 */
	static class ListNode {
		int			val;
		ListNode	next;

		ListNode(int x) {
			val = x;
		}

		@Override
		public String toString() {
			if (this.next != null) {
				return val + "---" + this.next.toString();
			} else {
				return val + "";
			}
		}
	}

	//画图找规律1-2-3-4  变为  2-1-4-3  四个为一组----324msA    其实也可以两个一组的,为了不new一个对象我也是蛮拼的。。。。
	public static ListNode swapPairs(ListNode head) {
		if (head == null || head.next == null) {
			return head;
		} else {
			ListNode temp = head.next;
			while (head != null) {
				ListNode a = head;
				if (a.next != null) {
					ListNode b = a.next;
					if (b.next != null) {
						ListNode c = b.next;
						if (c.next != null) {
							ListNode d = c.next;
							if (d != null) {
								c.next = (d.next == null) ? null : ((d.next.next == null) ? d.next : d.next.next);
								head = d.next;
								d.next = c;
								a.next = d;
								b.next = a;
								continue;
							}
						} else {
							b.next = a;
							a.next = c;
							c.next = null;
							break;
						}
					} else {
						b.next = a;
						a.next = null;
						break;
					}
				} else {
					break;
				}
			}
			return temp;
		}
	}

	//https://leetcode.com/discuss/31508/share-my-accepted-java-solution
	public static ListNode swapPairs1(ListNode head) {
		ListNode dummy = new ListNode(0);
		dummy.next = head;
		ListNode cur = dummy;
		ListNode l1 = null, l2 = null;
		while (cur.next != null && cur.next.next != null) {
			l1 = cur.next;
			l2 = cur.next.next;
			l1.next = l2.next;
			l2.next = l1;
			cur.next = l2;
			cur = l1;
		}
		return dummy.next;
	}

	//https://leetcode.com/discuss/29123/my-simple-java-solution-for-share
	public static ListNode swapPairs2(ListNode head) {
		ListNode dummy = new ListNode(0);
		dummy.next = head;
		ListNode current = dummy;
		while (current.next != null && current.next.next != null) {
			ListNode first = current.next;
			ListNode second = current.next.next;
			first.next = second.next;
			current.next = second;
			current.next.next = first;
			current = current.next.next;
		}
		return dummy.next;
	}

	//两个一组
	public static ListNode swapPairs3(ListNode head) {
		ListNode dummy = new ListNode(0);
		dummy.next = head;
		ListNode current = dummy;
		while(current.next!=null&¤t.next.next!=null){
			
		}
		return dummy.next;
	}

	public static void main(String[] args) {
		ListNode a = new ListNode(1);
		ListNode b = new ListNode(2);
		ListNode c = new ListNode(3);
		ListNode d = new ListNode(4);
		ListNode e = new ListNode(5);
		ListNode f = new ListNode(6);
		ListNode g = new ListNode(7);
		ListNode h = new ListNode(8);
		a.next = b;
		b.next = c;
		c.next = d;
		d.next = e;
		e.next = f;
		f.next = null;
		g.next = null;
		h.next = null;
		//System.out.println("第一种:" + swapPairs(a));
		//System.out.println("第二种:" + swapPairs1(a));
		//System.out.println("第三种:" + swapPairs2(a));
		System.out.println("第四种:" + swapPairs3(a));
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值