public ListNode RotateRight(ListNode head, int k) { ListNode newHead = null; while (k != 0) { while (head != null) { if (head.next.next == null) { newHead = head.next; head.next = null; head = newHead; } head = head.next; } k--; } return(head.next); }
public ListNode RemoveElements(ListNode head, int val) { while (head != null && head.val == val) { head = head.next; } ListNode curr = head; while (curr != null && curr.next != null) { if (curr.next.val == val) { curr.next = curr.next.next; } else { curr = curr.next; } } return(head); }
public ListNode AddTwoNumbers(ListNode l1, ListNode l2) { ListNode head = new ListNode(0); ListNode res = head; int carry = 0; while (l1 != null || l2 != null) { int l1_val = (l1.val != null) ? l1.val : 0; int l2_val = (l2.val != null) ? l2.val : 0; int current_sum = l1_val + l2_val + carry; int last_digit = current_sum / 10; carry = current_sum / 10; ListNode new_node = new ListNode(last_digit); res.next = new_node; if (l1 != null) { l1 = l1.next; } if (l2 != null) { l2 = l2.next; } res = res.next; } if (carry > 0) { ListNode new_node = new ListNode(carry); res.next = new_node; res = res.next; } return(head.next); }
public ListNode GetIntersectionNode(ListNode headA, ListNode headB) { List <ListNode> visited = new List <ListNode>(); while (headA != null) { visited.Add(headA); headA = headA.next; } while (headB != null) { if (visited.Contains(headB)) { return(headB); } headB = headB.next; } return(null); }
public ListNode AddTwoNumbersExtra(ListNode l1, ListNode l2) { List <int> list1 = new List <int>(); List <int> list2 = new List <int>(); while (l1 != null) { list1.Add(l1.val); l1 = l1.next; } while (l2 != null) { list2.Add(l2.val); l2 = l2.next; } list1.Reverse(); list2.Reverse(); long sum = Int64.Parse(String.Join("", list1)) + Int64.Parse(String.Join("", list2)); if (sum == 0) { return(new ListNode(0)); } ListNode head = new ListNode(0); ListNode res = head; while (sum != 0) { res.next = new ListNode((int)sum % 10); sum = sum / 10; res = res.next; } return(head.next); }
public ListNode DeleteDuplicates(ListNode head) { if (head == null || head.next == null) { return(head); } ListNode p = head; while (p != null && p.next != null) { if (p.val == p.next.val) { p.next = p.next.next; } else { p = p.next; } } return(head); }
public ListNode OddEvenList(ListNode head) { ListNode init = head; ListNode res = new ListNode(0); ListNode resNode = res; int idx = 1; while (init != null) { if (idx % 2 == 1) { resNode.next = new ListNode(init.val); resNode = resNode.next; } init = init.next; idx++; } init = head; idx = 1; while (init != null) { if (idx % 2 == 0) { resNode.next = new ListNode(init.val); resNode = resNode.next; } init = init.next; idx++; } resNode.next = null; return(res.next); }
public ListNode RemoveNthFromEnd(ListNode head, int n) { ListNode start = new ListNode(0); ListNode slow = start; ListNode fast = start; slow.next = head; //Move fast in front so that the gap between slow and fast becomes n for (int i = 1; i <= n + 1; i++) { fast = fast.next; } //Move fast to the end, maintaining the gap while (fast != null) { slow = slow.next; fast = fast.next; } //Skip the desired node slow.next = slow.next.next; return(start.next); }