{ "question_id": 24, "question_title": "两两交换链表中的节点", "difficulty": "中等", "question_content": "

给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。

你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。

 

示例 1:

\"\"
输入:head = [1,2,3,4]
输出:
[2,1,4,3]

示例 2:

输入:head = []
输出:
[]

示例 3:

输入:head = [1]
输出:
[1]

 

提示:

 

进阶:你能在不修改链表节点值的情况下解决这个问题吗?(也就是说,仅修改节点本身。)

", "topic_link": "https://bbs.csdn.net/topics/600470117", "cpp": "#include \nusing namespace std;\nstruct ListNode\n{\n\tint val;\n\tListNode *next;\n\tListNode() : val(0), next(nullptr) {}\n\tListNode(int x) : val(x), next(nullptr) {}\n\tListNode(int x, ListNode *next) : val(x), next(next) {}\n};\nclass Solution\n{\npublic:\n\tListNode *swapPairs(ListNode *head)\n\t{\n\t\tstruct ListNode dummy, *prev = &dummy, *p = head;\n\t\tdummy.next = head;\n\t\twhile (p != nullptr && p->next != nullptr)\n\t\t{\n\t\t\tstruct ListNode *q = p->next;\n\t\t\tp->next = q->next;\n\t\t\tq->next = prev->next;\n\t\t\tprev->next = q;\n\t\t\tprev = p;\n\t\t\tp = p->next;\n\t\t}\n\t\treturn dummy.next;\n\t}\n};", "java": "public class ListNode {\n\tint val;\n\tListNode next;\n\tListNode(int x) {\n\t\tval = x;\n\t}\n}\nclass Solution {\n\tpublic ListNode swapPairs(ListNode head) {\n\t\tListNode list1 = new ListNode(0);\n\t\tlist1.next = head;\n\t\tListNode list2 = list1;\n\t\twhile (head != null && head.next != null) {\n\t\t\tlist2.next = head.next;\n\t\t\thead.next = list2.next.next;\n\t\t\tlist2.next.next = head;\n\t\t\tlist2 = list2.next.next;\n\t\t\thead = list2.next;\n\t\t}\n\t\treturn list1.next;\n\t}\n}", "js": "", "python": "class ListNode(object):\n\tdef __init__(self, x):\n\t\tself.val = x\n\t\tself.next = None\nclass LinkList:\n\tdef __init__(self):\n\t\tself.head=None\n\tdef initList(self, data):\n\t\tself.head = ListNode(data[0])\n\t\tr=self.head\n\t\tp = self.head\n\t\tfor i in data[1:]:\n\t\t\tnode = ListNode(i)\n\t\t\tp.next = node\n\t\t\tp = p.next\n\t\treturn r\n\tdef\tconvert_list(self,head):\n\t\tret = []\n\t\tif head == None:\n\t\t\treturn\n\t\tnode = head\n\t\twhile node != None:\n\t\t\tret.append(node.val)\n\t\t\tnode = node.next\n\t\treturn ret\nclass Solution(object):\n\tdef swapPairs(self, head):\n\t\tdummyHead = ListNode(-1)\n\t\tdummyHead.next = head\n\t\tprev, p = dummyHead, head\n\t\twhile p != None and p.next != None:\n\t\t\tq, r = p.next, p.next.next\n\t\t\tprev.next = q\n\t\t\tq.next = p\n\t\t\tp.next = r\n\t\t\tprev = p\n\t\t\tp = r\n\t\treturn dummyHead.next\n# %%\nl = LinkList()\nhead = [1,2,3,4]\nl1 = l.initList(head)\ns = Solution()\nprint(l.convert_list(s.swapPairs(l1)))", "status": 1, "keywords": "递归,链表", "license": { "cpp": "https://github.com/begeekmyfriend/leetcode", "python": "https://github.com/qiyuangong/leetcode", "java": "https://blog.csdn.net/FaustoPatton/article/details/82993032" }, "notebook": { "cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/23/23_cpp.ipynb?type=file", "python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/23/23_python.ipynb?type=file", "java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/23/23_java.ipynb?type=file" }, "notebook_enable": 1 }