[LintCode] Swap Two Nodes in Linked List 交换链表中的两个结点

Given a linked list and two values v1 and v2. Swap the two nodes in the linked list with values v1 and v2. It‘s guaranteed there is no duplicate values in the linked list. If v1 or v2 does not exist in the given linked list, do nothing.

Notice

You should swap the two nodes with values v1 and v2. Do not directly swap the values of the two nodes.

Example
Given 1->2->3->4->null and v1 = 2, v2 = 4.

Return 1->4->3->2->null.

时间: 2024-10-22 23:41:59

[LintCode] Swap Two Nodes in Linked List 交换链表中的两个结点的相关文章

LintCode "Swap Two Nodes in Linked List"

Nothing special. Just take care of corner cases. class Solution { public: /** * @param head a ListNode * @oaram v1 an integer * @param v2 an integer * @return a new head of singly-linked list */ ListNode* swapNodes(ListNode* head, int v1, int v2) { i

【LeetCode-面试算法经典-Java实现】【025-Reverse Nodes in k-Group(单链表中k个结点一组进行反转)】

[025-Reverse Nodes in k-Group(单链表中k个结点一组进行反转)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes i

用C语言把双向链表中的两个结点交换位置,考虑各种边界问题。

用C语言把双向链表中的两个结点交换位置,考虑各种边界问题. [参考] http://blog.csdn.net/silangquan/article/details/18051675

1.7交换链表中的相邻节点

交换链表中的相邻节点 题目描述: 把链表相邻元素翻转,例如给定链表为1-->2一>3一>4一>5-->6一>7,则翻转后的链表变为2一>1一>4一>3一>6一>5一>7 解题思路: 就地逆序法: 通过调整结点指针域的指向来直接调换相邻的两个结点.如果单链表恰好有偶数个结点,那么只需要将奇偶结点对调即可,如果链表有奇数个结点,那么只需要将除最后一个结点外的其它结点进行奇偶对调即可. 代码实现: # -*-coding:utf-8-*-

(Java) LeetCode 24. Swap Nodes in Pairs —— 两两交换链表中的节点

Given a linked list, swap every two adjacent nodes and return its head. Example: Given 1->2->3->4, you should return the list as 2->1->4->3. Note: Your algorithm should use only constant extra space. You may not modify the values in the

451 两两交换链表中的节点

原题网址:https://www.lintcode.com/problem/swap-nodes-in-pairs/description 描述 给一个链表,两两交换其中的节点,然后返回交换后的链表. 您在真实的面试中是否遇到过这个题?  是 样例 给出 1->2->3->4, 你应该返回的链表是 2->1->4->3. 挑战 你的算法只能使用常数的额外空间,并且不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 标签 链表 思路:要形成一个新链表,主要操作就

LeetCode237_Delete Node in a Linked List(删除链表中的节点) Java题解

题目: Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 -

每天AC系列(十):两两交换链表中的节点

1 题目 LeetCode第24题,交换链表的相邻节点. 2 直接交换 直接交换的思想很简单,遍历一次链表,进行两两交换. ListNode newHead = new ListNode(0); newHead.next = head; ListNode before = newHead; ListNode first = head; ListNode second = head.next; ListNode move; while(true) { move = second.next; fir

Swap Two Nodes in Linked List

Given a linked list and two values v1 and v2. Swap the two nodes in the linked list with values v1 and v2. It's guaranteed there is no duplicate values in the linked list. If v1 or v2 does not exist in the given linked list, do nothing. Notice You sh